R program to calculate Body Mass Index (BMI)
The Body Mass Index (BMI) is a quick way to assess your body size simply with your weight and height, regardless of your gender. Quickly calculate your BMI and find out which category you fall into.
The Body Mass Index (BMI) is the only index validated by the World Health Organization to assess an individual’s build and therefore health risks. The BMI makes it possible to determine whether one is the situation of thinness, overweight or obesity for example.
In this program we will calculate BMI. Body mass index (BMI) is a measure of body fat based on height and weight that applies to adult men and women.
R Code: Write a program that calculates and displays a person’s body mass index in R Lang
bmi.r file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
user.height <- 69 user.weight <- 150 user.height <- as.double(user.height) user.weight <- as.double(user.weight) user.bmi <- user.weight / user.height^2 * 703 print(c("BMI:",user.bmi),collapse=' '); if (user.bmi < 18.5) { print("underweight") } else if (user.bmi < 25) { print("normal") } else if (user.bmi < 30) { print("overweight") } else { print("obese") } |
The first two lines of code create variables called user.height
and user.weight
and assign them values of 69 and 150, respectively. The next two lines convert these variables to double precision floating point numbers.
The fifth line calculates the BMI of the user by dividing their weight by their height squared, and then multiplying the result by a conversion factor of 703. This equation is one way of calculating BMI, but it is important to note that there are other ways to calculate BMI and some variations in the specific conversion factor used.
After calculating the BMI, the code prints it to the console. It then enters a series of if
statements which check the value of the BMI and print a message indicating whether the person is underweight, normal weight, overweight, or obese.
Take inputs from user
bmi2.r file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
user.height <- readline(prompt="height (in inches)? ") user.weight <- readline(prompt="weight (in pounds)? ") user.height <- as.double(user.height) user.weight <- as.double(user.weight) user.bmi <- user.weight / user.height^2 * 703 print(c("BMI:",user.bmi),collapse=' '); if (user.bmi < 18.5) { print("underweight") } else if (user.bmi < 25) { print("normal") } else if (user.bmi < 30) { print("overweight") } else { print("obese") } |
Output:
The first two lines of code read in the user’s height and weight from the console and store them in variables called user.height
and user.weight
, respectively. The next two lines convert these variables to double precision floating point numbers.
The fifth line calculates the BMI of the user by dividing their weight by their height squared, and then multiplying the result by a conversion factor of 703. This equation is one way of calculating BMI, but it is important to note that there are other ways to calculate BMI and some variations in the specific conversion factor used.
After calculating the BMI, the code prints it to the console. It then enters a series of if
statements which check the value of the BMI and print a message indicating whether the person is underweight, normal weight, overweight, or obese.
You May Also Like:
[…] R Code to Calculate BMI […]