Thursday, June 19, 2014

Elementary Programming; Exercise No. 12

Body Mass Index (BMI) is a measure of health on weight. It can be calculated by taking your weight in kilograms and dividing by the square of your height in meters. Write a program that prompts the user to enter a weight in pounds and height in inches and displays the BMI. Note that one pound is  0.45359237 kilograms and one inch is  0.0254 meters.

Solution:

public static void main(String[] args) {
   Scanner s = new Scanner(System.in);
   double weight, height, bmi;
   final double POUND = 0.45359237;
   final double INCH = 0.0254;

   System.out.print("Enter weight in pounds:");
   weight = s.nextDouble();

   System.out.print("Enter height in inches:");
   height = s.nextDouble();

   weight = weight * POUND; // convert weight from pounds to kilograms
   height = height * INCH; // covert height from inches to meters

   bmi = weight/(Math.pow(height, 2));

   System.out.print("BMI is " + bmi);
}

No comments:

Post a Comment

Author