C++ Mini Projects BMI Calculator
Hey guys I'm back with another cool project. This going to be the third calculator in our series the first one with the basic calculator the second one was a student calculator and now this going to be a BMI calculator. I hope you're enjoying this series and gaining something from it. Now let's smash this project too!
Description: Project: BMI Calculator
Explanation..
Asking the user to input thier height ( meters) and weight (kilograms)
Calculating bmi( body mass index) with the help of formula
Bmi= weight/height *height
Categorize bmi into different health
Options
Underweight ...bmi<18.5
Normal weight...18.5</bmi<24.9
Overweight...25</bmi <29.9
Obesity...bmi >/30
You will learn throughout
Validates the input...
Making sure that height and weight are positive numbers
Calculations...calculate bmi
Conditional statements...
Categorizing bmi.
Code to build the desired project
#include <iostream>
#include <iomanip> // For setting decimal precision
using namespace std;
int main() {
double weight, height, bmi;
// Input: Weight and height
cout << "BMI Calculator\n";
cout << "--------------\n";
// Input weight
cout << "Enter your weight in kilograms: ";
cin >> weight;
while (weight <= 0) {
cout << "Invalid weight. Please enter a positive number: ";
cin >> weight;
}
// Input height
cout << "Enter your height in meters: ";
cin >> height;
while (height <= 0) {
cout << "Invalid height. Please enter a positive number: ";
cin >> height;
}
// Calculate BMI
bmi = weight / (height * height);
// Display BMI with precision
cout << fixed << setprecision(2); // Limit output to 2 decimal places
cout << "\nYour BMI is: " << bmi << endl;
// Determine health category
if (bmi < 18.5) {
cout << "Category: Underweight\n";
} else if (bmi >= 18.5 && bmi < 24.9) {
cout << "Category: Normal weight\n";
} else if (bmi >= 25 && bmi < 29.9) {
cout << "Category: Overweight\n";
} else {
cout << "Category: Obesity\n";
}
return 0;
}
Explanation of the code
Input of height and weight
cout << "Enter your weight in kilograms: ";
cin >> weight;
while (weight <= 0) {
cout << "Invalid weight. Please enter a positive number: ";
cin >> weight;
}
Asking the user for his/her height and weight. Using the while loop to validate the input, making sure that height and weight input are positive numbers.
Calculation of bmi...
bmi = weight / (height * height);
Height is squared for matching the bmi formula
Displaying the bmi...
cout << fixed << setprecision(2); // Limit output to 2 decimal places
cout << "Your BMI is: " << bmi << endl;
Setprecision(2): For making sure that bmi is correctly displayed with two decimal places, for the purpose of readability.
Determination of health category...
if (bmi < 18.5) {
cout << "Category: Underweight\n";
} else if (bmi >= 18.5 && bmi < 24.9) {
cout << "Category: Normal weight\n";
} else if (bmi >= 25 && bmi < 29.9) {
cout << "Category: Overweight\n";
} else {
cout << "Category: Obesity\n";
}
Using if-else statements for comparing of bmi values against given thresholds.
Assignment of health category according to bmi.
Sample running of the program
Example of input and output
BMI Calculator
--------------
Enter your weight in kilograms: 68
Enter your height in meters: 1.75
Your BMI is: 22.20
Category: Normal weight
Example 2
BMI Calculator
--------------
Enter your weight in kilograms: 85
Enter your height in meters: 1.70
Your BMI is: 29.41
Category: Overweight
I hope you like the idea and built the program on your own. If minor errors come across your way, then don't be discouraged because there are a million of resources from which you can get help including popular websites like stack overflow and nowadays AI. Keep building something great!
See you next time with another cool project!
Comments
Post a Comment