BMI Calculator Program in C++
#include <iostream>
using namespace std;
int main() {
float weight, height, bmi;
cout << "Enter weight in kilograms: ";
cin >> weight;
cout << "Enter height in meters: ";
cin >> height;
bmi = weight / (height * height);
cout << "BMI is: " << bmi << endl;
if (bmi < 18.5) {
cout << "Underweight" << endl;
} else if (bmi < 25) {
cout << "Normal weight" << endl;
} else if (bmi < 30) {
cout << "Overweight" << endl;
} else {
cout << "Obesity" << endl;
}
return 0;
}
Explanation:
#include <iostream>
: Includes the input/output stream library to perform input and output operations.using namespace std;
: Allows the usage of entities such as cout, cin, etc. from the standard namespace std.int main() { ... }
: The main function where the program execution begins.float weight, height, bmi;
: Declares three variables of type float to store weight, height, and BMI.cout << "Enter weight in kilograms: ";
: Outputs a message to prompt the user to enter weight.cin >> weight;
: Takes input from the user for weight and stores it in the 'weight' variable.cout << "Enter height in meters: ";
: Outputs a message to prompt the user to enter height.cin >> height;
: Takes input from the user for height and stores it in the 'height' variable.bmi = weight / (height height);
: Calculates BMI using the formula BMI = weight / (height height).cout << "BMI is: " << bmi << endl;
: Outputs the calculated BMI.if (bmi < 18.5) { ... } else if (bmi < 25) { ... } else if (bmi < 30) { ... } else { ... }
: Checks the BMI value and displays corresponding messages indicating underweight, normal weight, overweight, or obesity.return 0;
: Indicates successful termination of the program.