Java program to calculate Body Mass Index (BMI)
The Body Mass Index (BMI) is a quick way to assess your body size simply with your weight and height, regardless of your gender. Quickly calculate your BMI and find out which category you fall into.
The Body Mass Index (BMI) is the only index validated by the World Health Organization to assess an individual’s build and therefore health risks. The BMI makes it possible to determine whether one is the situation of thinness, overweight or obesity for example.
In this program we will calculate BMI. Body mass index (BMI) is a measure of body fat based on height and weight that applies to adult men and women.
Java Code: how do you calculate bmi in java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | import java.util.Date; import java.util.Scanner; public class JavaExamples { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.println("Enter next person's information:"); System.out.print("height (in inches)? "); double height = console.nextDouble(); System.out.print("weight (in pounds)? "); double weight = console.nextDouble(); System.out.println(); double bmi = weight / Math.pow(height, 2) * 703; System.out.println("BMI:"+bmi); if (bmi < 18.5) { System.out.print("underweight"); } else if (bmi < 25) { System.out.print("normal"); } else if (bmi < 30) { System.out.print("overweight"); } else { System.out.print("obese"); } } } |
This program is written in Java and it calculates the body mass index (BMI) of a person based on their height and weight. Here’s what the code does, step by step:
- The program starts by importing two classes from the Java library:
java.util.Scanner
andjava.util.Date
. TheScanner
class is used to take input from the user, and theDate
class is used to retrieve the current date and time. - The program defines a main method, which is the entry point for a Java program.
- Inside the main method, the program creates a new
Scanner
object to read input from the user. Then it prompts the user to enter their height in inches and weight in pounds. - Next, the program calculates the user’s body mass index (BMI) using the formula: BMI = weight / height^2 * 703
- The program then uses a series of
if-else
statements to check the value of the BMI and determine whether the person is underweight, normal, overweight, or obese. - Finally, it prints the result on the console.
This code calculates the BMI of a person and shows the result as underweight, normal, overweight or obese depending on the value of the BMI.
The output of this program would be the result of the BMI calculation for a person and the result of whether the person is underweight, normal, overweight, or obese based on the value of their BMI, This output will only be visible after you run the code by using any Java compiler.
The output would be a string of text on the console as the result of the calculation. for example, if the input values entered by the user are height: 65 and weight: 150 the output would be “overweight”
1 2 3 4 5 6 7 8 | Enter next person's information: height (in inches)? 65 weight (in pounds)? 150 BMI:24.958579881656807 normal |
You May Also Like:
[…] BMI Calculator Program in Java […]