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:

  1. #include <iostream>: Includes the input/output stream library to perform input and output operations.

  2. using namespace std;: Allows the usage of entities such as cout, cin, etc. from the standard namespace std.

  3. int main() { ... }: The main function where the program execution begins.

  4. float weight, height, bmi;: Declares three variables of type float to store weight, height, and BMI.

  5. cout << "Enter weight in kilograms: ";: Outputs a message to prompt the user to enter weight.

  6. cin >> weight;: Takes input from the user for weight and stores it in the 'weight' variable.

  7. cout << "Enter height in meters: ";: Outputs a message to prompt the user to enter height.

  8. cin >> height;: Takes input from the user for height and stores it in the 'height' variable.

  9. bmi = weight / (height height);: Calculates BMI using the formula BMI = weight / (height height).

  10. cout << "BMI is: " << bmi << endl;: Outputs the calculated BMI.

  11. 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.

  12. return 0;: Indicates successful termination of the program.