Calculate Body Mass Index (BMI) in Java



The Body Mass Index is the body mass in kilogram divided by the square of body height in meters. This is expressed as kg/m^2.

A Java program that calculates the Body Mass Index (BMI) is given as follows.

Example

import java.util.Scanner;
public class Example {
   public static void main(String args[]) {
      Scanner sc = new Scanner(System.in);
      System.out.print("Input weight in kilogram: ");
      double weight = sc.nextDouble();
      System.out.print("\nInput height in meters: ");
      double height = sc.nextDouble();
      double BMI = weight / (height * height);
      System.out.print("\nThe Body Mass Index (BMI) is " + BMI + " kg/m2");
   }
}

Output

Input weight in kilogram: 55
Input height in meters: 1.5
The Body Mass Index (BMI) is 24.444444444444443 kg/m2

Now let us understand the above program.

The values of the weight and height are obtained from the user in kilogram and meters respectively. The code snippet that demonstrates this is given as follows −

Scanner sc = new Scanner(System.in);
System.out.print("Input weight in kilogram: ");
double weight = sc.nextDouble();
System.out.print("\nInput height in meters: ");
double height = sc.nextDouble();

Then the Body Mass Index is calculated using the formula BMI = weight / (height * height). Finally the values of the Body Mass Index is displayed. The code snippet that demonstrates this is given as follows −

double BMI = weight / (height * height);
System.out.print("\nThe Body Mass Index (BMI) is " + BMI + " kg/m2");
Updated on: 2024-05-31T13:04:55+05:30

29K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements