The Body Mass Index (BMI) is defined as ratio of the weight of a person(in kilograms) to the square of the height(in meters). Write a C program that receives weight and height, calculates the BMI, and reports the BMI category as per the following table:
BMI Category | BMI |
---|---|
Starvation | < 15 |
Anorexic | 15.1 to 17.5 |
Underweight | 17.6 to 18.5 |
Ideal | 18.6 to 24.9 |
Overweight | 25 to 25.9 |
Obese | 30 to 30.9 |
Morbidly Obese | >= 40 |
Related Read:
else if statement in C
Relational Operators In C
Logical Operators In C
Formula To Calculate Body Mass Index (BMI)
bmi = weight / (height x height);
Logic To Calculate BMI and BMI Category
We ask the user to enter weight in kelogram(kg) and height in meters(m). Then using above formula we calculate BMI(Body Mass Index). Next, by referring to the chart below we display the BMI category.
For BMI Category Ideal:
bmi >= 18.6 && bmi <= 24.9
Expected Output for the Input
User Input:
Enter height in meter
1.4
Enter weight in kg
60
Output:
Your Body Mass Index(BMI) is 30.612246
Your BMI category is: Obese
Video Tutorial: C Program To Calculate BMI and BMI Category
[youtube https://www.youtube.com/watch?v=Lz_o-l5vNwA]
Source Code: C Program To Calculate BMI and BMI Category
#include < stdio.h > int main() { float height, weight, bmi; printf("Enter height in meter\n"); scanf("%f", &height); printf("Enter weight in kg\n"); scanf("%f", &weight); bmi = weight / (height * height); printf("Your Body Mass Index(BMI) is %f\n", bmi); if(bmi < 15) { printf("Your BMI category is: Starvation\n"); } else if(bmi >= 15.1 && bmi <= 17.5) { printf("Your BMI category is: Anorexic\n"); } else if(bmi >= 17.6 && bmi <= 18.5) { printf("Your BMI category is: Underweight\n"); } else if(bmi >= 18.6 && bmi <= 24.9) { printf("Your BMI category is: Ideal\n"); } else if(bmi >= 25 && bmi <= 25.9) { printf("Your BMI category is: Overweight\n"); } else if(bmi >= 30 && bmi <= 30.9) { printf("Your BMI category is: Obese\n"); } else if(bmi >= 40) { printf("Your BMI category is: Morbidly Obese\n"); } else { printf("Wrong entry\n"); } return 0; }
Output 1:
Enter height in meter
0.8
Enter weight in kg
80
Your Body Mass Index(BMI) is 125.000000
Your BMI category is: Morbidly Obese
Output 2:
Enter height in meter
1.4
Enter weight in kg
50
Your Body Mass Index(BMI) is 25.510204
Your BMI category is: Overweight
For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List
For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert