C Program to accept the height of a person in centimeter & categorize

For a better understanding, we always recommend you to learn the basic topics of C programming listed below:

How to categorize persons related to height?

In this c program, we need to arrange the persons according to their height in centimeters and categorizes them as taller, dwarf & average for that, we need to accept the heights from the user. Check the height of each user using the if conditional statement. If the height is less than 150 cm, we categorize them as short. If the height is between 150 and 165 cm, it is called average height. If the height is between 165 and 195, it falls into the category 'taller people'. all the above sizes are the tallest category.

The logic of this program first declares the variable 'ht' as a float type. Read the height in centimeters from the user and save it into the variable 'ht'. now check if (ht<150), then we display the height is short. Using else if (ht<150 and ht<=165), then display average height. Otherwise, check else if (ht>=165.0 and ht<=195), then display taller. Else show abnormal height.

What is the syntax of the if-else statement?


if (testExpression) {

  // codes inside the body of if

} else {

  // codes inside the body of else

}

If the test expression is true, we execute the code inside the if condition and skip the else part. But if the test expression is false, the reverse is happening; in this program, we have to categorize the persons according to their height. By using the if-else condition, we evaluate the comparison of heights.

ALGORITHM

STEP 1: Include the header files to use the built-in header files into the C program.

STEP 2: Declare the variable ht as the float.

STEP 3: Read the Height of the user.

STEP 4: Check if "ht<150" Display the short.

STEP 5: Check using else if "ht>=150" and "ht<=165" then display Average height.else do step 6.

STEP 7: Else check if "ht>=165" and "ht<=195" then display Taller. else do step 7.

STEP 8: Display abnormal height.

C Source Code

                                          #include <stdio.h>

void main() {
  float ht;
  printf("Enter  the Height (in centimeters)\n");
  scanf("%f", & ht);
  if (ht < 150)
    printf("short\n");
  else if ((ht >= 150) && (ht <= 165.0)) /* using if we compare the heights */
printf(" Average Height\n");
else if ((ht >= 165.0) && (ht <= 195.0))
  printf("Taller\n");
else
  printf("Abnormal height\n");
} /* End of main() */
                                      

OUTPUT

Enter  the Height (in centimeters)

45
Dwarf