Welcome dear readers! Are You conscious about your health and usually get worried when you feel obese? In this article, we will write a **c++ BMI program** that will tell you whether you are obese, underweight, or fit. It will help us to practice our **variables** and **conditional statements** understanding. The program will ask the user to enter his weight and height. Then the program will calculate the BMI(body mass index) of the user according to his height and weight. Finally, the program will print a final statement about the health of the user. Now let’s learn how to build a **BMI calculator program in c++**.

### What is Body Mass Index(BMI)?

The calculation of body weight with respect to the height of a person to indicate his/her fitness is called BMI. The BMI measurement is used to access a person’s fitness based on his height and weight. BMI is calculated by dividing a person’s weight by the square of his/her height where weight and height must be in kilograms and meters.

BMI = weight in kilograms / Height in meters

- If the BMI value is less than 18.5, the person is underweight.
- The value between 18.5 to 24.9 is optimal and indicates a healthy and fit body.
- If the BMI value is between 25 to 29.9, the person is overweight.
- BMI values greater than 30 indicate obesity.

Based on the above-stated information, now let’s build a BMI calculator in c++.

## How to build a BMI calculator in c++?

- First of All, declare and define a function “CalculateBMI”. Take height and weight as parameters in kilograms and meters. Dividing the weight by the square of height to calculate BMI. The function returns a float type variable “BMI” containing the result of the calculation.

float CalculateBMI(float h, float w) { float SqrtOfHeight = h * h; float BMI = w / SqrtOfHeight; return BMI; }

- Take two variables to take the values of user weight and height in kilogram and meters.
- Print proper messages to take input from the user.

float height; float weight; cout << "Please enter your height in meters>>"; cin >> height; cout << "please enter your weight in kg>>"; cin >> weight;

- Call the function CalculateBMI and pass height and weight as arguments. Store the result in a floating-point variable.

float BMI = CalculateBMI(height, weight); cout << "your BMI is " << BMI << "\n";

- Now apply the first check. If the BMI value is less than 18.5, tell the user that he is underweight.

if (BMI < 18.5) { cout << "you are underweight."; }

- If the values of BMI lies between 18.5 to 24.9, print a message to the user that he is healthy.

else if (BMI>18.5 & BMI < 24.9) { cout << "Congrats! You are healthy"; }

- But If the value is greater than 25, inform the user that he is overweight.

else if(BMI>25 & BMI < 29.9) { cout << "You are overweight"; }

- And if the value is higher than 30, warn the user that he is obese.

else if(BMI>30) { cout << "You are Obese"; }

## C++ BMI calculator when height is in meters

Now Check out the complete code of the program to understand things in more detail.

#include<iostream> using namespace std; float CalculateBMI(float h, float w) { float SqrtOfHeight = h * h; float BMI = w / SqrtOfHeight; return BMI; } int main() { float height; float weight; cout << "Please enter your height in meters>>"; cin >> height; cout << "please enter your weight in kg>>"; cin >> weight; float BMI = CalculateBMI(height, weight); cout << "your BMI is " << BMI << "\n"; if (BMI < 18.5) { cout << "you are underweight.Thin and skinny"; } else if (BMI>18.5 & BMI < 24.9) { cout << "Congrats! You are healthy"; } else if(BMI>25 & BMI < 29.9) { cout << "You are overweight"; }else if(BMI>30) { cout << "You are Obese"; } return 0; }

## C++ program to calculate BMI when height is in inches

Now let’s do some discussion on the conversion of units. The above-explained program takes height in meters but we should also discuss programs that take height in inches, feet, cm. We can also take weight in grams or pounds. For this, we have to do some unit conversion. I will just explain a program that takes height in inches to give you an idea. You can do the rest of the conversion yourself to practice your understanding.

- We know one meter is equal to 39.37 inches. So, to convert meters into inches divide it with 39.37.
- The calculateBMI function will be a little bit different this time. Multiply the square of 39.37 with weight as we have square of height in the denominator.
- Now make a change in the cout statement and write inches instead of meters.

Now see the complete code of the second **BMI program.**

#include<iostream> using namespace std; float CalculateBMI(float h, float w) { float BMI = (w *1550.0047)/ (h * h ); return BMI; } int main() { float height; float weight; cout << "Please enter your height in inches>>"; cin >> height; cout << "please enter your weight in kg>>"; cin >> weight; float BMI = CalculateBMI(height, weight); cout << "your BMI is " << BMI << "\n"; if (BMI < 18.5) { cout << "you are underweight"; } else if (BMI>18.5 & BMI < 24.9) { cout << "Congrats! You are healthy"; } else if(BMI>25 & BMI < 29.9) { cout << "You are overweight"; }else if(BMI>30) { cout << "You are Obese"; } return 0; }

For further practice, must try the same program with weight in pounds or height in feet. I hope you found the post informative and interesting. Thanks for reading and supporting.