C Programs Tutorials | IT Developer
IT Developer

Java Programs



Share with a Friend

Introduction to Java

BMI Calculator - Java Program

import java.util.Scanner;

 

public class BMICalculator {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

 

        System.out.println("===== BMI CALCULATOR =====");

        System.out.print("Enter your weight (in kilograms): ");

        double weight = sc.nextDouble();

 

        System.out.print("Enter your height (in meters): ");

        double height = sc.nextDouble();

 

        // BMI Formula: BMI = weight / (height × height)

        double bmi = weight / (height * height);

 

        System.out.println("\nYour BMI is: " + bmi);

 

        // Classifying BMI according to standard ranges

        if (bmi < 18.5) {

            System.out.println("Category: Underweight");

        } else if (bmi >= 18.5 && bmi < 24.9) {

            System.out.println("Category: Normal weight");

        } else if (bmi >= 25 && bmi < 29.9) {

            System.out.println("Category: Overweight");

        } else {

            System.out.println("Category: Obese");

        }

    }

}

Output

 
OUTPUT :
===== BMI CALCULATOR =====
Enter your weight (in kilograms): 70
Enter your height (in meters): 1.75

Your BMI is: 22.857142857142858
Category: Normal weight

Explanation of the Program

Import the Scanner class

import java.util.Scanner;

Used for taking user inputs.

Take Weight and Height as Input

double weight = sc.nextDouble();

double height = sc.nextDouble();

  • Weight in kilograms
  • Height in meters

Apply BMI Formula

BMI formula:

BMI = weight / (height × height)

Output the BMI

System.out.println("Your BMI is: " + bmi);

Classify According to BMI Ranges

Using if–else ladder:

  • <18.5 → Underweight
  • 18.5–24.9 → Normal
  • 25–29.9 → Overweight
  • 30+ → Obese