What is BMI?
BMI or Body Mass Index is body mass measurement of an individual based on their height and weight. Using BMI a person can be classified into underweight, normal, overweight or obese etc according to the table below.
Formula of BMI
BMI = kg/m2
where kg is the weight of individual and m2 is the squared height of individual
Formula of BMI in Lbs
BMI = lbs/in2
where lbs is the weight of individual and in2 is the squared height of individual
Here in this tutorial, you will learn how to write:
1) Simple Java Program to Calculate BMI of an Individual
2) Advance Java Program to Calculate BMI of an Individual and print if the individual is underweight, normal, overweight or obese according to following BMI categories
- Less than 15 = Very severely underweight
- bmi >= 15 but bmi < 16 = Severely underweight
- bmi >= 16 but bmi < 18.5 = Underweight
- bmi >= 18.5 but bmi < 25 = Normal (healthy weight)
- bmi >= 25 but bmi < 30 = Overweight
- bmi >= 30 but bmi < 35 = Moderately obese
- bmi >= 35 but bmi < 40 = Severely obese
- bmi > 40 = Very severely obese
1) Java Program to Calculate BMI
//Java program to calculate the BMI
//Importing the Scanner package
import java.util.Scanner;
//Main / Drived Class
public class Main
{
//Main Function
public static void main(String[] args) {
//Creating the object of Scanner Class
Scanner input = new Scanner(System.in);
//Giving the hint to user what has to enter
System.out.println("Enter your weight unit (kg or lbs): ");
//Taking Weight Unit from user
String unit = input.nextLine();
//Giving the hint to user what has to enter
System.out.println("Enter your Weight : ");
//Taking weight from user
double weight = input.nextDouble();
double height = 0;
//Checking which weight unit has been selected by user
if(unit.equals("kg")){
//If unit is kg
//Giving the hint to user what has to enter
System.out.println("Enter your Height(In Meters) : ");
//Taking height(in Meters) from user
height = input.nextDouble();
}else if(unit.equals("lbs")){
//Giving the hint to user what has to enter
System.out.println("Enter your Height(In Inches) : ");
//Taking height(in Inches) from user
height = input.nextDouble();
}
//Declaring a double type parameter
double bmi;
/*Switch Cases on weight units because BMI Formula changes according to
/* the Weight Units
*/
switch (unit){
//If unit is kg
case "kg":
//caluclate the bmi with formula and store it in variable
bmi = weight / (height * height);
//print the BMI
System.out.println("YOUR BMI IS : "+ bmi +" kg/m2");
break;
//If unit is lbs
case "lbs":
//caluclate the bmi with formula and store it in variable
bmi = 703 * (weight / (height * height));
//print the BMI
System.out.println("YOUR BMI IS : "+ bmi +" lbs/in2");
break;
default:
System.out.println("Please choose correct weight unit");
}
}
}
Output
Enter your weight unit (kg or lbs):
lbs
Enter your Weight :
170
Enter your Height(In Inches) :
69
YOUR BMI IS : 25.101869355177485 lbs/in2
2) Java Program to Calculate BMI & Health Status
//Java program to calculate the BMI
//Importing the Scanner package
import java.util.Scanner;
//Main / Drived Class
public class Main
{
//Main Function
public static void main(String[] args) {
//Creating the object of Scanner Class
Scanner input = new Scanner(System.in);
//Giving the hint to user what has to enter
System.out.println("Enter your weight unit (kg or lbs): ");
//Taking Weight Unit from user
String unit = input.nextLine();
//Giving the hint to user what has to enter
System.out.println("Enter your Weight : ");
//Taking weight from user
double weight = input.nextDouble();
double height = 0;
//Checking which weight unit has been selected by user
if(unit.equals("kg")){
//If unit is kg
//Giving the hint to user what has to enter
System.out.println("Enter your Height(In Meters) : ");
//Taking height(in Meters) from user
height = input.nextDouble();
}else if(unit.equals("lbs")){
//Giving the hint to user what has to enter
System.out.println("Enter your Height(In Inches) : ");
//Taking height(in Inches) from user
height = input.nextDouble();
}
//Declaring a double type parameter
double bmi = 0;
/*Switch Cases on weight units because BMI Formula changes according to
/* the Weight Units
*/
switch (unit){
//If unit is kg
case "kg":
//caluclate the bmi with formula and store it in variable
bmi = weight / (height * height);
//print the BMI
System.out.println("YOUR BMI IS : "+ bmi +" kg/m2");
break;
case "lbs":
//caluclate the bmi with formula and store it in variable
bmi = 703 * (weight / (height * height));
//print the BMI
System.out.println("YOUR BMI IS : "+ bmi +" lbs/in2");
break;
default:
System.out.println("Please choose correct weight unit");
}
//Cheking bmi with BMI CATEGORIES
if(bmi < 15){
System.out.println("You are in 'Very severely underweight' Category");
}else if(bmi >= 15 && bmi < 16){
System.out.println("You are in 'Severely underweight' Category");
}else if(bmi >= 16 && bmi < 18.5){
System.out.println("You are in 'Underweight' Category");
}else if(bmi >= 18.5 && bmi < 25){
System.out.println("You are in 'Normal (healthy weight)' Category");
}else if(bmi >= 25 && bmi < 30){
System.out.println("You are in 'Overweight' Category");
}else if(bmi >= 30 && bmi < 35){
System.out.println("You are in 'Moderately obese' Category");
}else if(bmi >= 35 && bmi < 40){
System.out.println("You are in 'Severely obese' Category");
}else if(bmi > 40){
System.out.println("You are in 'Very severely obese' Category");
}
}
}
Output
Enter your weight unit (kg or lbs):
kg
Enter your Weight :
123
Enter your Height(In Meters) :
1.98
YOUR BMI IS : 31.37434955616774 kg/m2
You are in 'Moderately obese' Category