Java Program to Calculate BMI (Body Mass Index)

Previously we have developed a Java program for Simple calculator using switch cases and methods. Now in this post, we will develop a Java program for a BMI calculator.

The BMI stands for Body Mass Index. It is a measure of health based on height and weight. It can be calculated by taking the weight in kilograms and dividing it by the square of your height in meters.

Formula for Calculating BMI in Metric Units,

BMI = (Weight in Kg) / (Height in Meters * Height in Meters)

For example,
weight = 75 kg, height = 1.5 m
BMI = 75 / (1.5*1.5) = 33.33

Using the range of BMI, individuals are classified as underweight, normal or overweight. Its value is in a specific range for a healthy individual. The following table shows the main BMI categories,

BMI RangeCategory
< 18.5Thinness
18.5 – 25Normal
25 – 30Overweight
> 30Obese

BMI calculator finds whether the person is underweight, normal, overweight, or obese. The person who wants to lose their want can use a weight loss calculator which can tell the daily caloric requirements, and many easy tips to lose weight.

Java program for BMI calculator

import java.util.Scanner;

public class BMICalculator {

   // method to check bmi category
   public static String bmiCategory(double weight, 
                                    double height) {

      // calculate bmi
      double bmi = weight / ( height * height) ;

      // check range
      if(bmi < 18.5)
         return "Thinness";
      else if(bmi < 25)
         return "Normal";
      else if(bmi < 30)
         return "Overweight";
      else
         return "Obese";
   }

   public static void main(String[] args) {

      // declare variables
      double weightInKg = 0.0f;
      double heightInMeters = 0.0f;
      String result = null;

      // create Scanner class object to
      // take input
      Scanner scan = new Scanner(System.in);
      System.out.print("Enter weight in Kg: ");
      weightInKg = scan.nextDouble();
      System.out.print("Enter height in meters: ");
      heightInMeters = scan.nextDouble();

      // calculate BMI index
      result = bmiCategory( weightInKg, 
                           heightInMeters );

      // display result
      System.out.println(result);

      // close Scanner class object
      scan.close();
   }
}

The output for the different-test cases are:-

Enter weight in Kg: 45
Enter height in meters: 1.37
Normal

Enter weight in Kg: 80
Enter height in meters: 1.7
Overweight

Normally we measure height in feet and inches. So, you can take the height in feet and inches and later convert it into the meters. Below code converts feet and inches to meters.

int feet;
int inches;

System.out.println("Enter height in feet and inches,");
System.out.print("Feet:: ");
feet = scan.nextInt();

System.out.print("Inches:: ");
inches = scan.nextInt();

// Convert feet and inches to meter
heightInMeters = ((feet * 12) + inches) * 0.0254;

Output:-

Enter weight in Kg: 60
Enter height in feet and inches,
Feet:: 5
Inches:: 4
Normal

Similar Java programming examples

Leave a Comment

Your email address will not be published. Required fields are marked *