Mathematical
Silver Coder
Simply because I was bored and because I felt like it, I have converted @Master Yoda's code into C. If you prefer C mainly due to how lightweight, fast, and cleaner it is, then you can use this version that I've written. The major difference in this one, is that it uses the metric system instead of the imperial system. Here's the code:
The only differences in this version, is that of course, it's written in C, so there is a change in the syntax. Not only that though, I have commented the code so that it can be understood(So if you lose this post but still have the code, you can refer to these comments to learn how the code works). Along with that, I have also simply cleaned the code up, as I've spaced things out and have removed the braces that are generally unnecessary in the if-else if-statement.
If at any point you want to modify the C version to use the imperial system, refer to these sites:
@Master Yoda's original copy that was written in C++ can be found here: https://www.codeforum.org/threads/how-i-built-my-bmi-calculator-in-c.809/
C:
/*
* BMI Calculator
* Originally written by Master Yoda on Codeforum.org
* Converted to C by Mathematical on Codeforum.org
*/
#include <stdio.h>
int main(void) {
float Height, BMI;
int Weight;
printf("Welcome to the BMI calculator.\n");
// Ask the user to enter both their weight and height.
printf("Please enter your weight(kg): ");
scanf("%d", &Weight);
printf("Please enter your height(m): ");
scanf("%f", &Height);
printf("Weight: %d kg\n", Weight);
printf("Height: %f m\n", Height);
// Perform the calculations and print off the result.
BMI = Weight / (Height * Height);
printf("BMI: %f\n", BMI);
// Determine to see if the user's BMI matches up with a certain class.
if(BMI <= 18.5)
printf("Underweight\n");
else if(BMI <= 24.9)
printf("Normal\n");
else if(BMI <= 29.9)
printf("Overweight\n");
else
printf("Obese\n");
return 0;
}
The only differences in this version, is that of course, it's written in C, so there is a change in the syntax. Not only that though, I have commented the code so that it can be understood(So if you lose this post but still have the code, you can refer to these comments to learn how the code works). Along with that, I have also simply cleaned the code up, as I've spaced things out and have removed the braces that are generally unnecessary in the if-else if-statement.
If at any point you want to modify the C version to use the imperial system, refer to these sites:
- https://www.thecalculatorsite.com/articles/health/bmi-formula-for-bmi-calculations.php - BMI formula
- https://www.unitconverters.net/length/meters-to-inches.htm - Goes over how to convert metres into inches. The site also contains resources for other types of conversions.
@Master Yoda's original copy that was written in C++ can be found here: https://www.codeforum.org/threads/how-i-built-my-bmi-calculator-in-c.809/
Last edited: