BMI Calculator in Java
In this blog, we will learn to create a simple BMI calculator in Java programming language where the BMI value is calculated based on the inputs given by the user in metric units.
What is BMI?
BMI stands for Body Mass Index. It is used to measure the body mass based on the height and weight of the individual. The range of BMI classifies individuals as underweight, normal or overweight. The main BMI categories are shown below:
Formula for Calculating BMI in Metric Units
The formula for calculating BMI value is:
BMI = Weight in Kg/(Height in Meters * Height in Meters)
For example, let us consider height = 1.5m and weight = 90kg
BMI = 90 / (1.5 * 1.5) = 40 (Obese).
How It Works?
- Input the weight in kg and height in cm from the user.
- A static method CalculateBMI() is called by passing height and weight as its arguments.
- The BMI value is calculated by dividing weight in kg with square of the height in meters.
- The height in centimeters can be converted to height in meters by dividing the height value by 100 (1 m = 100 cm).
- The obtained BMI value is displayed on the console.
- Also, if-else statements are used to categorize the BMI values. So, according to the obtained BMI value, the if-else statements will be executed.
Here is the complete source code for Java program that calculates the BMI value based on the inputs given by the user in metric units. Also, note that the height input is to be given in centimeters.
Here is a sample output of the above program.
Are you looking to master coding and crack top company interviews? Go ahead and try GUVI’s Pro Subscription to help you with everything needed to build a strong Technical Career.
Know more about GUVI:
GUVI | Learn to code in your native language
Coding Bootcamp . New Age GURUKULAM 3 months Intensive Project Based Learning program, where you will learn and gain a…
Also, download GUVI’s app to learn the latest IT skills in vernacular languages.