The program below is the answer to Liang’s Introduction to Java Programming (9th Edition) Chapter 2 Exercise 2.14.
Question: Health application: computing BMI) 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.
1 2 |
/**<br /> *<br /> * @Author: Aghatise Osazuwa<br /> * Website: www.cscprogrammingtutorials.com<br /> *<br /> * Exercise 2.14 - Health Application: Computing BMI<br /> *<br /> */ <br /><br />import java.util.Scanner;<br /><br />public class ComputingBMI {<br /><br /> public static void main(String[] args) {<br /> <br /> //Display Program Information<br /> System.out.println("This Program Calculates Body Mass Index (BMI).n");<br /><br /> //create Scanner <br /> Scanner input = new Scanner(System.in);<br /><br /> //prompt user to enter details<br /> System.out.println("Enter weight in pounds:");<br /> double weight = input.nextDouble();<br /> System.out.println("Enter height in inches:");<br /> double height = input.nextDouble();<br /><br /> //calculate the BMI<br /> weight = weight * 0.45359237; //convert weight to kilograms<br /> height = height * 0.0254; //convert height to meters<br /><br /> double bMI = weight / (height * height);<br /> //format BMI to four decimal places<br /> bMI = (int) (bMI * 10000) / 10000.0;<br /><br /> //display the result<br /> System.out.println("BMI is " + bMI + "n");<br /> }<br />}<br /> |
Click here to see other solutions to Introduction to Java Programming.