Thursday, March 10, 2016

BMI Calculator in Java

a. Java Program for the body mass index (BMI) is commonly used by health and nutrition professionals to estimate human body fat in populations.

It is computed by taking the individual's weight (mass) in kilograms and dividing it by the square of their height in meters.

Write a Java program that reads from the keyboard the wight and the height in kilograms and meters and prints on the screen the BMI of the individual plus using this table gives a verdict:

BMI category
less than 18.5 underweight
18.5 to 24.9 normal weight
25.0 to 29.9 overweight
30.0 or more obese
b. Extend the program at a. to

1. read the name of the person from the keyboard

2. give the choice of the wight to be input either in kilograms or in pounds

3. give the choice of the height to be given either in meters or in feet

Perform the conversions from lb to Kg or from feet to meters and using the table above give the verdict.

Code Solution:

import java.util.Scanner;

public class BMI {

public static void main(String[] args) {
// TODO Auto-generated method stub
final double KILOGRAMS_PER_POUND = 0.453;
    //final double METERS_PER_INCH = 0.026;
        double weight = 0.0;
        double height = 0.0;
        double bmi = 0.0;
String name;
        String weightType = null;
        String heightType =null;

System.out.println("Enter your Name: ");
Scanner readname = new Scanner(;
name = readname.nextLine();

        System.out.println("Press 1 or 2 to choose Weight Type: ");
        Scanner readweightType = new Scanner(;
        weightType =;
        System.out.println("Enter Weight in Kilograms:");
        else if(weightType.equalsIgnoreCase("2"))
        System.out.println("Enter Weight in Pounds:");
        System.out.println("Invalid WeightType Entry");
        Scanner getWeight = new Scanner(;
        weight = getWeight.nextDouble();
        weight = weight* KILOGRAMS_PER_POUND ;
        //convert pounds to KG
        System.out.println("Press 1 or 2 to choose Height Type: ");
        Scanner readHeightType = new Scanner(;
        heightType =;
        System.out.println("Enter Height in Meters:");
        else if(heightType.equalsIgnoreCase("2"))
        System.out.println("Enter Weight in Feet:");
        System.out.println("Invalid HeightType Entry");
        Scanner readHeight = new Scanner(;
        height = readHeight.nextDouble();
        height = height/3.2808;
        bmi = Math.abs((weight)/(height * height));
System.out.print("\n \t Your Name is " +name);
        System.out.printf("\n \t The BMI is %f", bmi);
        if(bmi < 18.5)
        System.out.print(" belongs to UnderWeight");
        else if(bmi >= 18.5 && bmi < 24.9)
          System.out.print(" belongs to NormalWeight");
        else if(bmi >= 25.0 && bmi <= 29.9)
          System.out.print(" belongs to OverWeight");
        else if(bmi >= 30.0 )
          System.out.print(" belongs to Obese");


No comments:

Post a Comment