BMI Calculator Program in C++
Body Mass Index is a simple calculation using a person’s height and weight.
The formula is BMI = kg/m2 where kg is a person’s weight in kilograms and m2 is their height in metres squared.
A BMI of 25.0 or more is overweight, while the healthy range is 18.5 to 24.9.
BMI applies to most adults 18-65 years.
solution:
#include<iostream>
#include<iomanip>
using namespace std;
void main()
{
double Weight, height, BMI;
//Ask user to enter weight.
cout << "=========================================\n\n";
cout << setw(30) << "Enter Weight in pound: ";
cin >> Weight;
//Ask user to enter height.
cout << "\n=========================================\n\n";
cout << setw(30) << "Enter height in inches: ";
cin >> height;
cout << "\n=========================================\n\n";
//const kg = 0.45359237.
const double Kg_per_pound = 0.45359237;
//const height = 0.025.
const double Mtr_per_inch = 0.025;
//weight * Kg_per_pound.
double WeightInKg = Weight * Kg_per_pound;
//height * Mtr_per_inch.
double heightInInches = height * Mtr_per_inch;
//BMI = Kg / m2.
BMI = WeightInKg / (heightInInches * heightInInches);
//Display BMI.
cout << setw(18) << "BMI: " << BMI << endl;
cout << "\n=========================================\n\n";
//if()......else().
if (BMI < 18.5)
cout << setw(22) << "Underweight." << endl;
else if (18.5 <= BMI < 25.0)
cout << setw(22) << "Normal." << endl;
else if (25.0 <= BMI < 30.0)
cout << setw(22)<< "Overweight." << endl;
else if (30.0 <= BMI)
cout << setw(22)<< "Obese." << endl;
cout << "\n=========================================\n";
}
No comments:
Post a Comment