Program to Calculate Body Mass Index (BMI)
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;
float BMI( float height, float weight)
{
float bmi = weight / pow (height, 2);
return bmi;
}
int main()
{
float height = 1.79832;
float weight = 70;
float bmi = BMI(height, weight);
cout << "The BMI is " << setprecision(15) << bmi
<< " so " ;
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;
}
|
Java
import java.io.*;
class GFG
{
public static double BMI( double height, double weight)
{
double bmi = weight / Math.pow(height, 2 );
return bmi;
}
public static void main(String[] args)
{
double height = 1.79832 ;
double weight = 70 ;
double bmi = BMI(height, weight);
System.out.print( "The BMI is " + bmi + " so " );
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" );
}
}
|
Python3
def BMI(height, weight):
bmi = weight / (height * * 2 )
return bmi
height = 1.79832
weight = 70
bmi = BMI(height, weight)
print ( "The BMI is" , format (bmi), "so " , end = '')
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#
using System;
public class GFG {
public static double BMI( double height, double weight)
{
double bmi = weight / Math.Pow(height, 2);
return bmi;
}
static public void Main()
{
double height = 1.79832;
double weight = 70;
double bmi = BMI(height, weight);
Console.Write( "The BMI is " + bmi + " so " );
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" );
}
}
|
Javascript
<script>
function BMI(height, weight){
let bmi = weight/Math.pow(height, 2);
return bmi;
}
let height = 1.79832;
let weight = 70;
let bmi = BMI(height, weight);
process.stdout.write( "The BMI is " + bmi + " so " );
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" );
</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
Share your thoughts in the comments
Please Login to comment...