Write a program that calculates and displays a person’s body mass index (bmi).: In the previous article, we have seen Java Program to Count Total Number of Divisors of a Number
In this article we are going to see how to find body mass index using Java programming language.
Java Program to Calculate Body Mass Index
Body Mass Index:
Body Mass Index(BMI) which is a number calculated from a person’s weight and height. It gives an indication regarding person’s fatness. From BMI we can get to know what needs to be the proper fatness with respect to weight and height. It helps to keep track on weight according to height so that health problems can be avoided.
Let’s see different ways to find body mass index.
Method-1: Java Program to Calculate Body Mass Index By Using Static Input Value
Approach:
- Assign value to WEIGHT and HEIGHT.
- Calculate BMI as weight / height2.
- Print the result.
Program:
import java.util.Scanner; public class Main { public static void main(String[] args) { //weight in kilograms double weight = 70; //height in meters double height = 1.72; // calculate bmi double bmi = weight / (height * height); // display bmi System.out.println("Your BMI is " + bmi); String outcome=""; if (bmi < 18.5) { outcome = "UNDERWEIGHT"; } else if (bmi < 25) { outcome = "NORMAL"; } else if (bmi < 30) { outcome = "OVERWEIGHT"; } else if (bmi < 35) { outcome = "OBESE CLASS I"; } else if (bmi < 40) { outcome = "OBESE CLASS II"; } else { outcome = "OBESE CLASS III"; } System.out.println("Your BMI result indicates " + outcome); } }
Output: Your BMI is 23.661438615467823 Your BMI result indicates NORMAL
Method-2: Java Program to Calculate Body Mass Index By Using User Input Value
Approach:
- Create scanner class object.
- Take user input for WEIGHT and HEIGHT.
- Calculate BMI as weight / height2.
- Print the result.
Program:
import java.util.Scanner; public class Main { public static void main(String[] args) { // create scanner class object Scanner sc = new Scanner(System.in); // ask user to enter weight in kilograms System.out.print("Enter your weight in kilograms: "); double weight = sc.nextDouble(); // ask user to enter height in meters System.out.print("Enter your height in meters: "); double height = sc.nextDouble(); // calculate bmi double bmi = weight / (height * height); // display bmi System.out.println("Your BMI is " + bmi); String outcome=""; if (bmi < 18.5) { outcome = "UNDERWEIGHT"; } else if (bmi < 25) { outcome = "NORMAL"; } else if (bmi < 30) { outcome = "OVERWEIGHT"; } else if (bmi < 35) { outcome = "OBESE CLASS I"; } else if (bmi < 40) { outcome = "OBESE CLASS II"; } else { outcome = "OBESE CLASS III"; } System.out.println("Your BMI result indicates " + outcome); } }
Output: Enter your weight in kilograms: 70 Enter your height in meters: 1.72 Your BMI is 23.661438615467823 Your BMI result indicates NORMAL
Method-3: Java Program to Calculate Body Mass Index By Using User Defined Method
Approach:
- Create scanner class object.
- Take user input for WEIGHT and HEIGHT.
- Call
findbmi()
method by passingweight
andheight
as parameter. - Inside method calculate BMI as weight / height2.
- Print the result.
Program:
import java.util.Scanner; public class Main { public static void main(String[] args) { // create scanner class object Scanner sc = new Scanner(System.in); // ask user to enter weight in kilograms System.out.print("Enter your weight in kilograms: "); double weight = sc.nextDouble(); // ask user to enter height in meters System.out.print("Enter your height in meters: "); double height = sc.nextDouble(); //calling findbmi() findbmi(weight,height); } //user defined method findbmi() to find BMI public static void findbmi(double weight, double height) { // calculate bmi double bmi = weight / (height * height); // display bmi System.out.println("Your BMI is " + bmi); String outcome=""; if (bmi < 18.5) { outcome = "UNDERWEIGHT"; } else if (bmi < 25) { outcome = "NORMAL"; } else if (bmi < 30) { outcome = "OVERWEIGHT"; } else if (bmi < 35) { outcome = "OBESE CLASS I"; } else if (bmi < 40) { outcome = "OBESE CLASS II"; } else { outcome = "OBESE CLASS III"; } System.out.println("Your BMI result indicates " + outcome); } }
Output: Enter your weight in kilograms: 95 Enter your height in meters: 1.72 Your BMI is 32.11195240670633 Your BMI result indicates OBESE CLASS I
Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic.
Related Java Programs: