In this post, we will learn to code the Java Program to Calculate BMI (Body Mass Index). Let’s understand about Body Mass Index and How can we calculate BMI or Body Mass Index in Java Programming Language.
BMI or Body Mass Index
BMI stands for Body Mass Index, which is used to measure the body fat based on height and weight that applies to men and women accordingly.
BMI is calculated by taking the weight in kilograms and dividing it by the square of height in meters.
BMI Formula
The formula to calculate BMI is as follows,
BMI = (Weight in KG) / (Height in meters) x (Height in meters)
For example,
Weight = 70kg , Height = 168cm = 1.68m
BMI = 70 / (1.68 * 1.68) = 24.8
Now, when the BMI is calculated we refer to a range of BMI to classify whether the person is underweight, normal, overweight, or obese. The classification is given below,
BMI Range | Category |
less than 18.5 | Under Weight |
18.5 – 25 | Normal |
25 – 30 | Over Weight |
more than 30 | Obese |
Now, let’s see the Java Program to Calculate BMI (Body Mass Index).
Java Program to Calculate BMI (Body Mass Index)
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); System.out.println("Enter weight in kg : "); float weight = sc.nextFloat(); System.out.println("Enter height in meters : "); float height = sc.nextFloat(); float BMI = weight / (height * height); if(BMI < 18.5){ System.out.println("The BMI score is "+BMI+" .The person is Under Weight."); }else if(BMI >= 18.5 && BMI < 25){ System.out.println("The BMI score is "+BMI+" .The person is Normal."); }else if(BMI >= 25 && BMI < 30){ System.out.println("The BMI score is "+BMI+" .The person is Over Weight."); }else if(BMI >= 30){ System.out.println("The BMI score is "+BMI+" .The person is Obese."); } } }
Output
Enter weight in kg : 75
Enter height in meters: 1.68
The BMI score is 26.573132 .The person is Over Weight.
How Does This Program Work ?
Scanner sc = new Scanner(System.in); System.out.println("Enter weight in kg : "); float weight = sc.nextFloat(); System.out.println("Enter height in meters : "); float height = sc.nextFloat();
In this program, we have taken the input of weight in kg and height in meters from the user and stored it in the variable weight and height of datatype float respectively, using the Scanner Class in Java.
float BMI = weight / (height * height); if(BMI < 18.5){ System.out.println("The BMI score is "+BMI+" .The person is Under Weight."); }else if(BMI >= 18.5 && BMI < 25){ System.out.println("The BMI score is "+BMI+" .The person is Normal."); }else if(BMI >= 25 && BMI < 30){ System.out.println("The BMI score is "+BMI+" .The person is Over Weight."); }else if(BMI >= 30){ System.out.println("The BMI score is "+BMI+" .The person is Obese."); }
Then, we calculate the BMI using the formula discussed above. Then we used the if-else-if ladder to print the classification of the body according to the BMI range calculated.
This is the Java Program to Calculate BMI (Body Mass Index).
Conclusion
I hope after going through this post, you understand how to code Java Program to Calculate BMI (Body Mass Index).
If you have any doubt regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.
Also Read: