Search

# Calculate BMI in Java | AskTheCode

Updated: Mar 9

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

Problem:

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: ### Input:

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

### Output:

The first line displaying BMI up to 2 decimal points.

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

Code:

```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.in);

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);
System.out.printf("%.2f",bmi);
System.out.println();

if (bmi >= 15 && bmi < 19)
System.out.println("Underweight");
else if (bmi < 25)
System.out.println("Normal");
else if (bmi < 30)
System.out.println("Overweight");
}
}```

### 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