Introduction
Body Mass Index (BMI) is a measurement that helps assess whether a person has a healthy body weight for a given height.
It is calculated using a simple formula: BMI = weight (kg) / height² (m²). A BMI score falls into different categories
that indicate whether a person is underweight, normal weight, overweight, or obese. This program will help you calculate
your BMI using the formula and classify your weight status based on your BMI value.
Objective
The objective of this program is to create a simple BMI calculator in C. It will allow the user to input their weight
and height, then calculate their BMI and classify their body weight according to established categories.
Program Code
#include int main() { // Declare variables for weight, height, and BMI float weight, height, bmi; // Prompt the user to enter their weight in kilograms printf("Enter weight in kilograms: "); scanf("%f", &weight); // Prompt the user to enter their height in meters printf("Enter height in meters: "); scanf("%f", &height); // Calculate BMI using the formula: BMI = weight / (height * height) bmi = weight / (height * height); // Display the calculated BMI printf("Your BMI is: %.2f\n", bmi); // Classify BMI and display the appropriate message if (bmi < 18.5) { printf("You are underweight.\n"); } else if (bmi >= 18.5 && bmi < 24.9) { printf("You have a normal weight.\n"); } else if (bmi >= 25 && bmi < 29.9) { printf("You are overweight.\n"); } else { printf("You are obese.\n"); } return 0; }
Program Explanation
The program works as follows:
- The program starts by declaring three variables: weight, height, and bmi.
- The user is asked to input their weight in kilograms and height in meters.
- The BMI is calculated using the formula: bmi = weight / (height * height).
- The program then prints the calculated BMI value with two decimal places using printf.
- Based on the calculated BMI, the program classifies the body weight into one of four categories:
- Underweight if BMI is less than 18.5.
- Normal weight if BMI is between 18.5 and 24.9.
- Overweight if BMI is between 25 and 29.9.
- Obese if BMI is 30 or higher.
- Finally, the classification message is displayed to the user.
How to Run the Program
To run the program, follow these steps:
- Copy the provided C code into a text file and save it with a .c extension (e.g., bmi_calculator.c).
- Open a terminal or command prompt and navigate to the directory where the .c file is saved.
- Compile the program using a C compiler, for example using GCC:
gcc bmi_calculator.c -o bmi_calculator
- Run the compiled program:
./bmi_calculator
- Follow the on-screen prompts to enter your weight and height, and the program will display your BMI and classification.