Open In App

Program to Calculate Body Mass Index (BMI)

Improve
Improve
Like Article
Like
Save
Share
Report

The Body Mass Index (BMI) or Quetelet index is a value derived from the mass (weight) and height of an individual, male or female. The BMI is defined as the body mass divided by the square of the body height and is universally expressed in units of kg/m2, resulting from the mass in kilograms and height in meters. The formula is: 
 

BMI = (mass or weight)/(height*height)
where,
mass or weight is in Kg,
height is in meters

Examples: 
 

Input : height(in meter): 1.79832
weight(in Kg): 70
Output : The BMI is 21.64532402096181, so Healthy.
Explanation : 70/(1.79832*1.79832)

Input : height(in meter): 1.58496
weight(in Kg): 85
Output : The BMI is 33.836256857260594 so Suffering from Obesity
Explanation : 70/(1.58496*1.58496)

 

 

C++




#include <iomanip>
#include <iostream>
#include <math.h>
using namespace std;
  
// C++ program to illustrate
// how to calculate BMI
float BMI(float height, float weight)
{
    float bmi = weight / pow(height, 2);
    return bmi;
}
int main()
{
    float height = 1.79832;
    float weight = 70;
    // Function call
    float bmi = BMI(height, weight);
    cout << "The BMI is " << setprecision(15) << bmi
         << " so ";
    // Conditions to find out BMI category
    if (bmi < 18.5)
        cout << "underweight";
  
    else if (bmi >= 18.5 && bmi < 24.9)
        cout << "Healthy";
  
    else if (bmi >= 24.9 && bmi < 30)
        cout << "overweight";
  
    else if (bmi >= 30)
        cout << "Suffering from Obesity";
  
    return 0;
}
  
// This code is contributed by aarohirai2616.


Java




// Java code to solve the problem
import java.io.*;
  
class GFG
{
    
    // Java program to illustrate
    // how to calculate BMI
    public static double BMI(double height, double weight)
    {
        double bmi = weight / Math.pow(height, 2);
        return bmi;
    }
    // Driver code
    public static void main(String[] args)
    {
        double height = 1.79832;
        double weight = 70;
        
        // Function call
        double bmi = BMI(height, weight);
        System.out.print("The BMI is " + bmi + " so ");
        
        // Conditions to find out BMI category
        if (bmi < 18.5)
            System.out.print("underweight");
  
        else if (bmi >= 18.5 && bmi < 24.9)
            System.out.print("Healthy");
  
        else if (bmi >= 24.9 && bmi < 30)
            System.out.print("overweight");
  
        else if (bmi >= 30)
            System.out.print("Suffering from Obesity");
    }
}
  
// This code is contributed by aarohirai2616.


Python3




#Python program to illustrate 
# how to calculate BMI
def BMI(height, weight):
    bmi = weight/(height**2)
    return bmi
  
# Driver code
height = 1.79832
weight = 70
  
# calling the BMI function
bmi = BMI(height, weight)
print("The BMI is", format(bmi), "so ", end='')
  
# Conditions to find out BMI category
if (bmi < 18.5):
    print("underweight")
  
elif ( bmi >= 18.5 and bmi < 24.9):
    print("Healthy")
  
elif ( bmi >= 24.9 and bmi < 30):
    print("overweight")
  
elif ( bmi >=30):
    print("Suffering from Obesity")


C#




// C# code to solve the problem
using System;
  
public class GFG {
  
  // function to calculate BMI
  public static double BMI(double height, double weight)
  {
    double bmi = weight / Math.Pow(height, 2);
    return bmi;
  }
  
  static public void Main()
  {
  
    // Code
    double height = 1.79832;
    double weight = 70;
  
    // Function call
    double bmi = BMI(height, weight);
    Console.Write("The BMI is " + bmi + " so ");
  
    // Conditions to find out BMI category
    if (bmi < 18.5)
      Console.Write("underweight");
  
    else if (bmi >= 18.5 && bmi < 24.9)
      Console.Write("Healthy");
  
    else if (bmi >= 24.9 && bmi < 30)
      Console.Write("overweight");
  
    else if (bmi >= 30)
      Console.Write("Suffering from Obesity");
  }
}
  
// This code is contributed by lokesh.


Javascript




<script>
// Javascript program to illustrate
    // how to calculate BMI
    function BMI(height, weight){
    let bmi = weight/Math.pow(height, 2);
    return bmi;
    }
    let height = 1.79832;
        let weight = 70;
          
        // Function call
        let bmi = BMI(height, weight);
        process.stdout.write("The BMI is " + bmi + " so ");
          
        // Conditions to find out BMI category
        if (bmi < 18.5)
            console.log("underweight");
  
        else if (bmi >= 18.5 && bmi < 24.9)
            console.log("Healthy");
  
        else if (bmi >= 24.9 && bmi < 30)
           console.log("overweight");
  
        else if (bmi >= 30)
            console.log("Suffering from Obesity");
              
            // This code is contributed by aarohirai2616.
  </script>


Output: 
 

The BMI is 21.64532402096181 so Healthy

Time complexity: O(1) since constant operations are done

Auxiliary Space: O(1)
 



Last Updated : 17 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads