In: Computer Science
Java - Write a program to calculate a user’s BMI and display his/her weight status. The status is defined as Underweight, Normal, Overweight and obese if BMI is less than 18.5, 25, 30 or otherwise respectively. You need to read weight (in kilogram) and height (in metre) from the user as inputs. The BMI is defined as the ratio of the weight and the square of the height. [10 marks]
Java code:
import java.util.Scanner;
public class Main{
   public static void main(String[] args){
   Scanner input=new Scanner(System.in);
   //initializing weight and height
   float weight,height;
   //asking for weight
   System.out.print("Enter weight: ");
   //accepting it
   weight=input.nextFloat();
   //asking for height
   System.out.print("Enter height: ");
   //accepting it
   height=input.nextFloat();
   //finding bmi
   float bmi=weight/(height*height);
   //checking if it is less than 18.5
   if(bmi<18.5f)
   //printing Underweight
   System.out.println("Underweight");
   //checking if it is less than 25.0
   else if(bmi<25.0f)
   //printing Normal
   System.out.println("Normal");
   //checking if it is less than 30.0
   else if(bmi<30.0f)
   //printing Overweight
   System.out.println("Overweight");
   else
   //printing Obese
   System.out.println("Obese");
   }
}
Screenshot:

Input and Output:
