- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java program to calculate Body Mass Index (BMI)
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 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("
Input height in meters: "); double height = sc.nextDouble(); double BMI = weight / (height * height); System.out.print("
The 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("
Input 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("
The Body Mass Index (BMI) is " + BMI + " kg/m2");
Advertisements
To Continue Learning Please Login
Login with Google