Tuesday, September 27, 2016

// // Leave a Comment

Java Health Application: Computing BMI

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.

/**
 *
 * @Author: Aghatise Osazuwa
 * Website: www.cscprogrammingtutorials.com
 *
 * Exercise 2.14 - Health Application: Computing BMI
 *
 */ 

import java.util.Scanner;

public class ComputingBMI {

    public static void main(String[] args) {
        
        //Display Program Information
        System.out.println("This Program Calculates Body Mass Index (BMI).\n");

        //create Scanner 
        Scanner input = new Scanner(System.in);

        //prompt user to enter details
        System.out.println("Enter weight in pounds:");
        double weight = input.nextDouble();
        System.out.println("Enter height in inches:");
        double height = input.nextDouble();

        //calculate the BMI
        weight = weight * 0.45359237; //convert weight to kilograms
        height = height * 0.0254; //convert height to meters

        double bMI = weight / (height * height);
        //format BMI to four decimal places
        bMI = (int) (bMI * 10000) / 10000.0;

        //display the result
        System.out.println("BMI is " + bMI + "\n");
    }
}

Click here to see other solutions to Introduction to Java Programming.

0 comments:

Post a Comment


DISCLAIMER: Opinions expressed in comments are those of the comment writers alone and does not reflect or represent the views of the post author. We reserve the right to delete any post deemed inappropriate or offensive and/or spammy. Please do not use abusive words/hate speech.

I Appreciate your valuable Feedback. So, Please DO NOT SPAM - Spam comments will be deleted immediately.

Don't use brand name in name field and you're not allowed to use links in comments unless it's necessary. Such comments will be removed immediately.

Thanks.