Introduction:
Body Mass Index (BMI) is a measurement that helps assess whether an individual has a healthy body weight in relation to their height. It is commonly used to classify individuals into categories such as underweight, normal weight, overweight, and obese. BMI is calculated using the formula:
BMI = weight (kg) / height^2 (m^2)
In this program, we will compute the BMI based on the user’s input for their weight and height. The program will then categorize the result and provide feedback about the user’s weight status.
Objective:
The objective of this program is to:
- Accept user input for weight and height.
- Calculate the BMI using the provided formula.
- Classify the BMI value into one of the following categories: Underweight, Normal weight, Overweight, and Obese.
- Display the BMI result and the corresponding category to the user.
Program Code:
#include #include // for setting precision of output using namespace std; int main() { double weight, height, bmi; // Ask for user input cout << "Enter your weight in kilograms (kg): "; cin >> weight; cout << "Enter your height in meters (m): "; cin >> height; // Calculate BMI bmi = weight / (height * height); // Display BMI result with 2 decimal places cout << fixed << setprecision(2); cout << "Your BMI is: " << bmi << endl; // Determine and display BMI category if (bmi < 18.5) { cout << "You are underweight." << endl; } else if (bmi >= 18.5 && bmi < 24.9) { cout << "You have a normal weight." << endl; } else if (bmi >= 25 && bmi < 29.9) { cout << "You are overweight." << endl; } else { cout << "You are obese." << endl; } return 0; }
Explanation of Program Structure:
This program consists of the following steps:
- Input: The program prompts the user to input their weight (in kilograms) and height (in meters).
- Calculation: Using the inputted values, the program calculates the BMI using the formula BMI = weight / (height^2).
- Output: The program outputs the BMI with two decimal points, followed by a classification based on the calculated BMI value. The categories are:
- Underweight: BMI < 18.5
- Normal weight: 18.5 <= BMI < 24.9
- Overweight: 25 <= BMI < 29.9
- Obese: BMI >= 30
How to Run the Program:
To run the BMI calculator program, follow these steps:
- Write the program code in a C++ editor or IDE (such as Code::Blocks, Dev-C++, or Visual Studio).
- Save the program with a “.cpp” extension (e.g., bmi_calculator.cpp).
- Compile the program. Most IDEs provide a “Build” or “Compile” option. If you are using the command line, you can use the following command:
g++ bmi_calculator.cpp -o bmi_calculator
- Run the compiled program using the following command:
./bmi_calculator
- Enter your weight and height as prompted, and the program will calculate and display your BMI along with the classification.