Calculate BMI in Java | AskTheCode

Updated: Mar 9

Java program to calculate BMI and also interpret it | Java programming solution | Ask The Code


Write a Java program that reads values for weight in kilograms and height in centimeters and prints out the BMI ( Body Mass Index). Your program should also print on screen an interpretation of the BMI. Use the following scale:


A Line contains weight (in kg) and height (in cm) separated by a space.


The first line displaying BMI up to 2 decimal points.

Next line displaying the interpretation of the BMI according to the provided scale.


import java.util.*;

public class BMI_Calculator{
    /* Function for converting kg to pounds */
    public static double kilogramToPound(double kilogram){
        double pounds = kilogram * 2.2;
        return pounds;
    /* Function for converting cm to inches */
    public static double centimetresToInches(double centimetres){
        double inches = centimetres * 0.39;
        return inches;
    /* Function for calculating BMI */
    public static double bmi_calc(double weight, double height){
        double bmi = (weight * 703) / (height * height);
        return bmi;

    public static void main(String []args){
     	Scanner sc = new Scanner(;

        System.out.println("Enter your weight (in kg) and height (in cm) separated by a space: ");
     	String s = sc.nextLine();
        s = s.trim();

        double height = Double.valueOf(s.substring(s.indexOf(" ")+1,s.length()));
        double weight = Double.valueOf(s.substring(0,s.indexOf(" ")));

        weight = kilogramToPound(weight);
        height = centimetresToInches(height);

        double bmi = bmi_calc(weight, height);

        if (bmi >= 15 && bmi < 19)
        else if (bmi < 25)
        else if (bmi < 30)
20 views0 comments

Recent Posts

See All

Golf CodeChef Solution in Java - AskTheCode

CodeChef May Long Challenge Solution | Golf (LKDNGOLF) solution | AskTheCode Golf (LKDNGOLF) solution Problem: It's a lockdown. You’re bored in your house and are playing golf in the hallway. The hall

Solubility - CodeChef Solution in Java and C++| AskTheCode

CodeChef May Long Challenge Solution | Solubility (SOLBLTY) solution | AskTheCode Solubility (SOLBLTY) solution Problem: Suppose for a unit rise in temperature, the solubility of sugar in water increa

Valid Paths CodeChef Solution in C++ | AskTheCode

CodeChef May Long Challenge Solution | Valid Paths (VPATH) solution | AskTheCode Valid Paths (VPATH) CodeChef solution Problem: You are given a tree with N nodes numbered from 1 to N. A set S of nodes