Site icon TestingDocs.com

BMI Calculator Java Program

Introduction

In this post, we will write a simple java program BMI Calculator to calculate the BMI index of a person. The program takes weight and height as input.

 

BMI Formula

 BMI Index = 

 

IDE Setup

We will use Eclipse IDE in this tutorial. Some steps and instructions to create a Java application on Eclipse IDE:

https://www.testingdocs.com/create-a-new-java-project-in-eclipse/

https://www.testingdocs.com/create-java-package-in-eclipse-ide/

https://www.testingdocs.com/create-a-new-java-class-in-a-project/

https://www.testingdocs.com/run-java-project-in-eclipse/

 

Java Program

/**************************************************
 * BMICalculator.java
 * @program   	: 
 * @web        	: www.TestingDocs.com
 * @author      :  
 * @version     : 1.0
 **************************************************/

import java.util.Scanner;

public class BMICalculator {

  public static void main(String[] args) throws Exception {
    System.out.print("Please enter weight in kg:= ");
    Scanner keyboard = new Scanner(System.in);
    double weight = keyboard.nextDouble();
    System.out.print("Please enter height in meters:= ");
    double height = keyboard.nextDouble();

    double bmiIndex = calculateBMI(weight,height);

    System.out.println("The BMI index is: "+ bmiIndex);

    if(bmiIndex < 18.5) {
      System.out.println("Underweight");
    }else if (bmiIndex < 25) {
      System.out.println("Normal");
    }else if (bmiIndex < 30) {
      System.out.println("Overweight");
    }else {
      System.out.println("Obese");
    }
  }


  /**************************************************
   * Method to calculate BMI Index
   **************************************************/
  private static double calculateBMI(double weight,double height) {
    return (weight)/(height*height);
  }
}

 

Sample Output

Please enter weight in kg:= 86
Please enter height in meters:= 1.77
The BMI index is: 27.450604870886398
Overweight

 

Screenshot

 

Rounding the Result

https://www.testingdocs.com/questions/how-to-round-a-double-value-in-java/

Java Tutorials

Java Tutorial on this website:

https://www.testingdocs.com/java-tutorial/


For more information on Java, visit the official website :

https://www.oracle.com/in/java/

Exit mobile version