11.Create a program in C to determine the BMI category based on user input using 'switch-case'.
Purpose:
The purpose of this program is to calculate the Body Mass Index (BMI) of a person based on their weight and height input and determine their BMI category using a switch-case statement.
Agenda:
The agenda of this program is to provide a simple and efficient way to categorize individuals based on their BMI, which can help in assessing their health status and potential risks associated with their weight.
Real-life use cases:
This program can be used in healthcare settings, fitness centers, and personal health monitoring applications to quickly determine an individual's BMI category and provide insights into their overall health and wellness. It can also be used in research studies related to obesity, weight management, and related health conditions.
Write a program in C to determine the BMI category based on user input using 'switch-case'
/* a program in C to check if a year is a leap year using 'if-else' statements.*/
#include <stdio.h>
int main()
{
float weight, height, bmi;
printf("Enter your weight in kilograms: ");
scanf("%f", &weight);
printf("Enter your height in meters: ");
scanf("%f", &height);
bmi = weight / (height * height);
printf("Your BMI is: %.2f\n", bmi);
int category;
if (bmi < 18.5) {
category = 1;
} else if (bmi >= 18.5 && bmi < 25) {
category = 2;
} else if (bmi >= 25 && bmi < 30) {
category = 3;
} else {
category = 4;
}
switch (category) {
case 1:
printf("Underweight\n");
break;
case 2:
printf("Normal weight\n");
break;
case 3:
printf("Overweight\n");
break;
case 4:
printf("Obese\n");
break;
default:
printf("Invalid BMI category\n");
}
return 0;
}
Explanation:
1. The program first asks the user to input their weight in kilograms and height in meters.
2. It calculates the BMI using the formula BMI = weight / (height * height).
3. Based on the calculated BMI, it assigns a category number (1 for underweight, 2 for normal weight, 3 for overweight, and 4 for obese).
4. The program then uses a switch-case statement to print out the corresponding BMI category based on the category number assigned.