1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
#include <iostream> #include <iomanip> // For std::setprecision using namespace std; // Function to calculate BMI double calculateBMI(double weight, double height) { return weight / (height * height); } // Function to get BMI category string getBMICategory(double bmi) { if (bmi < 18.5) { return "Underweight"; } else if (bmi >= 18.5 && bmi < 24.9) { return "Normal weight"; } else if (bmi >= 25 && bmi < 29.9) { return "Overweight"; } else { return "Obesity"; } } int main() { double weight, height, bmi; // Prompt user for weight and height cout << "Enter weight (in kilograms): "; cin >> weight; cout << "Enter height (in meters): "; cin >> height; // Validate input if (weight <= 0 || height <= 0) { cerr << "Error: Weight and height must be positive numbers." << endl; return 1; } // Calculate BMI bmi = calculateBMI(weight, height); // Display BMI and category cout << fixed << setprecision(2); // Set decimal precision cout << "Your BMI is: " << bmi << endl; cout << "BMI Category: " << getBMICategory(bmi) << endl; return 0; } |