C Programs Tutorials | IT Developer
IT Developer

Java Programs



Share with a Friend

Operators & Expressions

Java Program: BMI Category Classification Using Chained Ternary

BMI Formula

 Java Programs

BMI Categories

BMI Range

Category

< 18.5

Underweight

18.5 – 24.9

Normal

25 – 29.9

Overweight

≥ 30

Obese

import java.util.Scanner;

 

public class BMICalculator {

    public static void main(String[] args) {

 

        Scanner sc = new Scanner(System.in);

 

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

        double weight = sc.nextDouble();

 

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

        double height = sc.nextDouble();

 

        double bmi = weight / (height * height);

 

        // Chained ternary operator for BMI classification

        String category = (bmi < 18.5) ? "Underweight" :

                          (bmi < 25)   ? "Normal" :

                          (bmi < 30)   ? "Overweight" :

                                         "Obese";

 

        System.out.println("\n------ BMI Result ------");

        System.out.printf("BMI Value : %.2f%n", bmi);

        System.out.println("Category  : " + category);

 

        sc.close();

    }

}

Output

 
OUTPUT 1:

INPUT : 
Enter weight in kg: 60
Enter height in meters: 1.70

OUTPUT : 
------ BMI Result ------
BMI Value : 20.76
Category  : Normal


OUTPUT 2:

INPUT : 
Enter weight in kg: 85
Enter height in meters: 1.65

OUTPUT : 
------ BMI Result ------
BMI Value : 31.22
Category  : Obese

Explanation

1. BMI Calculation

double bmi = weight / (height * height);

  • Uses the standard BMI formula.
  • double ensures decimal precision.

2. Chained Ternary Operator

String category = (bmi < 18.5) ? "Underweight" :

                  (bmi < 25)   ? "Normal" :

                  (bmi < 30)   ? "Overweight" :

                                 "Obese";

Equivalent if–else logic:

if (bmi < 18.5)

    category = "Underweight";

else if (bmi < 25)

    category = "Normal";

else if (bmi < 30)

    category = "Overweight";

else

    category = "Obese";

Demonstrates compact decision-making
Uses multiple conditions in a single expression

3. Output Formatting

System.out.printf("BMI Value : %.2f%n", bmi);

  • Displays BMI up to 2 decimal places.

Key Concepts Used

Arithmetic expressions
Chained ternary operator (?:)
Conditional logic without if–else
User input using Scanner
Formatted output

📌 Short Exam Answer

This program calculates BMI using height and weight, then classifies it into categories using a chained ternary operator, demonstrating concise conditional evaluation.