Skip to main content

Featured

say hello world with C++ - Solution in Hacker Rank - hackerranksolutions8

  Objective This is a simple challenge to help you practice printing to  stdout . You may also want to complete  Solve Me First  in C++ before attempting this challenge. We’re starting out by printing the most famous computing phrase of all time! In the editor below, use either  printf  or  cout  to print the string  Hello ,World!  to  stdout . The more popular command form is  cout . It has the following basic form: cout<<value_to_print<<value_to_print; Any number of values can be printed using one command as shown. The  printf  command comes from C language. It accepts an optional format specification and a list of variables. Two examples for printing a string are: printf("%s", string);   printf(string); Note that neither method adds a newline. It only prints what you tell it to. Output Format Print   Hello ,World!   to stdout. Sample Output Hello, World! Solution:- //Say Hello, ...

Boxes through tunnel in C Hackerrank solution

 You are transporting some boxes through a tunnel, where each box is a parallelepiped, and is characterized by its length, width and height.

The height of the tunnel 41 feet and the width can be assumed to be infinite. A box can be carried through the tunnel only if its height is strictly less than the tunnel’s height. Find the volume of each box that can be successfully transported to the other end of the tunnel. Note: Boxes cannot be rotated.

Input Format

The first line contains a single integer n, denoting the number of boxes.
n lines follow with three integers on each separated by single spaces  lengthwidth and height which are length, width and height in feet of the i-th box.

Constraints

  • < n < 100
  • < length, width, height < 100

Output Format

For every box from the input which has a height lesser than 41 feet, print its volume in a separate line.

Sample Input 0

4
5 5 5
1 2 40
10 5 41
7 2 42

Sample Output 0

125
80

Explanation 0

The first box is really low, only 5 feet tall, so it can pass through the tunnel and its volume is 5 x 5 x 5 = 125.

The second box is sufficiently low, its volume is 1 x 2 x 8 = 80.

The third box is exactly 41 feet tall, so it cannot pass. The same can be said about the fourth box.

Code

#include <stdio.h>
#include <stdlib.h>
#define MAX_HEIGHT 41

struct box
{
 /**
 * Define three fields of type int: length, width and height
 */
 int length;
 int width;
 int height;
}b;

typedef struct box box;

int get_volume(box b) {
 /**
 * Return the volume of the box
 */
 return (b.length*b.width*b.height);
}

int is_lower_than_max_height(box b) {
 /**
 * Return 1 if the box's height is lower than MAX_HEIGHT and 0 otherwise
 */
    if(b.height > 40)
 {
  return 0;
 }
 else
 {
  return 1;
 }
}

int main()
{
 int n;
 scanf("%d", &n);
 box *boxes = malloc(n * sizeof(box));
 for (int i = 0; i < n; i++) {
  scanf("%d%d%d", &boxes[i].length, &boxes[i].width, &boxes[i].height);
 }
 for (int i = 0; i < n; i++) {
  if (is_lower_than_max_height(boxes[i])) {
   printf("%d\n", get_volume(boxes[i]));
  }
 }
 return 0;
}

Comments