Table of contents |
|
Introduction |
|
Code for the BMI Calculator in C++ |
|
Understanding the Code |
|
How to Run the BMI Calculator |
|
A Body Mass Index (BMI) calculator helps determine whether a person is underweight, normal weight, overweight, or obese based on their height and weight. In this project, we will create a simple BMI calculator using C++. The program will take user input, calculate BMI, and categorize the result.
The formula for calculating BMI is:
However, if the weight is provided in pounds (lbs) and height in inches (in), we use:
This project involves:
#include <iostream>
#include <string>
using namespace std;
int main() {
// Variables for user input
string name;
double weight, height, BMI;
// Taking user input
cout << "Enter your name: ";
getline(cin, name);
cout << "Enter your weight in pounds: ";
cin >> weight;
cout << "Enter your height in inches: ";
cin >> height;
// BMI Calculation
BMI = (weight * 703) / (height * height);
// Displaying the BMI value
cout << "\n" << name << ", your BMI is: " << BMI << endl;
// Categorizing BMI
if (BMI > 0) {
if (BMI < 18.5)
cout << "You are underweight." << endl;
else if (BMI <= 24.9)
cout << "You have a normal weight." << endl;
else if (BMI < 29.9)
cout << "You are overweight. Consider a healthy diet and exercise." << endl;
else if (BMI < 34.9)
cout << "You are obese." << endl;
else if (BMI < 39.9)
cout << "You are severely obese." << endl;
else
cout << "You are morbidly obese." << endl;
} else {
cout << "Please enter valid inputs." << endl;
}
return 0;
}
1. Importing Libraries
#include <iostream>
#include <string>
iostream: Enables input and output operations.
string: Allows us to use string variables for handling names.
2. Declaring Variables and Taking Input
string name;
double weight, height, BMI;
name: Stores the user's name.
weight: Stores the user's weight in pounds.
height: Stores the user's height in inches.
BMI: Stores the calculated BMI.
User input is taken using:
cout << "Enter your name: ";
getline(cin, name);
cout << "Enter your weight in pounds: ";
cin >> weight;
cout << "Enter your height in inches: ";
cin >> height;
getline(cin, name): Reads the full name (including spaces).
cin >> weight; cin >> height;: Reads numerical inputs.
3. Calculating BMI
BMI = (weight * 703) / (height * height);
This formula converts weight and height into BMI.
4. Displaying BMI and Categorizing the Result
if (BMI > 0) {
if (BMI < 18.5)
cout << "You are underweight." << endl;
else if (BMI <= 24.9)
cout << "You have a normal weight." << endl;
else if (BMI < 29.9)
cout << "You are overweight. Consider a healthy diet and exercise." << endl;
else if (BMI < 34.9)
cout << "You are obese." << endl;
else if (BMI < 39.9)
cout << "You are severely obese." << endl;
else
cout << "You are morbidly obese." << endl;
} else {
cout << "Please enter valid inputs." << endl;
}
1. Open a C++ Compiler
2. Copy and Paste the Code
3. Compile and Run the Program
If using a command-line compiler, type:
g++ bmi_calculator.cpp -o bmi_calculator
4. Enter Your Details
Provide your name, weight (lbs), and height (inches).
5. Get Your BMI Result
The program will display your BMI and weight category.
This simple BMI calculator demonstrates how to take user input, perform calculations, and display meaningful results in C++. It is an excellent beginner-friendly project for learning basic I/O operations, conditional statements, and arithmetic calculations.
71 videos|48 docs|15 tests
|
1. What is a BMI Calculator and how does it work? | ![]() |
2. What are the benefits of using a BMI Calculator? | ![]() |
3. How do I implement a BMI Calculator in C++? | ![]() |
4. Can I run the BMI Calculator code on any C++ compiler? | ![]() |
5. What are some common errors to watch out for when coding a BMI Calculator in C++? | ![]() |