In this tutorial you will learn about the Create BMI (Body Mass Index) Calculator In Java and its application with practical example.
In this tutorial, we will learn to create a Create BMI (Body Mass Index) Calculator In Java using Java programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following Java programming topics:
- Java Operators.
- Basic Input and Output function in Java.
- Class and Object in Java.
- Basic Java programming. Basic Java programming.
- If-else statements in Java.
- For loop in Java.
What is Body Mass Index (BMI)
It is measured on the basis of “health,height and weight”. So it can be calculated by taking the weight in kg and by dividing the square of your height in meters.
Formula of Calculating BMI.
BMI = (Weight in Kg) / (Height in Meters * Height in Meters).
Example
weight = 70 kg, height = 1.2 m
BMI = 70 / (1.65*1.65) = BMI= 25.7 kg/m2
- Healthy BMI range is :=> 18.5 kg/m2 – 25 kg/m2
- Healthy weight for the height ranges :=> 50.4 kgs – 68.1 kgs
- You need to Lose 1.9 kgs to reach a BMI of 25 kg/m2.
BMI Range | Category |
BMI < 18.5 | Thinness |
BMI 18.5 – 25 | Normal |
BMI 25 – 30 | Overweight |
BMI> 30 | Obese |
Create BMI (Body Mass Index) Calculator In Java
In this program we will find BMI(Body Mass Index). We would first declared and initialized the required variables. Next, we would prompt user to input height and weight.Later we will find the BMI. Let’s have a look at the program.
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
// creating a java program to find BMI ... import java.util.Scanner; public class BMI{ // creating a method to calculate BMI.. public static String bmiCalculator(double weight, double height) { // calculating bmi double BMI = weight / ( height * height) ; // checking range according to result of BMI. if(BMI < 18.5) { System.out.println("BMI is ="+BMI); return "Thinness"; } else if(BMI < 25) { System.out.println("BMI is ="+BMI); return "Normal"; } else if(BMI < 30) { System.out.println("BMI is ="+BMI); return "Overweight"; } else { System.out.println("BMI is ="+BMI); return "Obese"; } } public static void main(String[] args) { // declaring variables.... double weight = 0.0f; double Meters = 0.0f; String result = null; // taking input form user Scanner scan = new Scanner(System.in); System.out.print("Enter weight in Kg: "); weight = scan.nextDouble(); System.out.print("Enter height in meters: "); Meters = scan.nextDouble(); // calling BMI function for calculating BMI index result = bmiCalculator(weight,Meters); // displaying the result... System.out.println(result); scan.close(); } } |
Output
BMI
In the above program, we have first declared and initialized a set variables required in the program.
- weight =It will hold value of kg.
- meter =It will hold value in meter.
- BMI = For calculating BMI.
After that taking value of weight in kg and height in meter we pass these values to function bmicalculator (weight,height).
After taking values from user bmicalculator () method takes two arguments and calculate bmi as shown in image below.
Here BMI calculator finds that whether the person is underweight, normal, overweight, or obese and the print result.