Classes/csci218/Assignments/BMI.c
2024-09-17 15:07:44 -04:00

70 lines
2.1 KiB
C

#include <stdio.h>
#include <math.h>
#include <string.h>
int main(){
// Declare Variables
float bmi, weight, height, mostellar, duBois, boyd, lifeExpect;
char gender[15];
char line[] = "********************************";
// Display Program Purpose
printf("%s%s%s\n", line, line, line);
printf("This program will calculate your BMI, Life Expectancy, and BSA(mulitple methods)\nwith just your weight(kg), height(cm), and gender.\n");
printf("%s%s%s\n", line, line, line);
// Read in weight, height, and gender from user
printf("Please enter your weight(kg): ");
scanf("%f", &weight);
printf("Please enter your height(cm): ");
scanf("%f", &height);
// Make sure correct values for gender are entered
while (1) {
printf("Please enter your gender(male or female): ");
scanf("%s", gender);
if (strcmp("male", gender) == 0 || strcmp("female", gender) == 0) {
break;
} else {
printf("Bad value: Please enter either male or female all lowercase\n");
}
}
// Calculate the BMI
bmi = 10000 * (weight / pow(height, 2));
// Calculate the BSA(Mosteller's)
mostellar = sqrt((height * weight) / 3600);
// Calculate the BSA(Dubois)
duBois = (0.20247 * pow((height / 100), 0.725)) * pow(weight, 0.425);
// Calculate the BSA(boyd)
boyd = (0.0003207 * pow(height, 0.3)) * pow((1000 * weight), (0.6721 - (0.0188 * log10(weight))));
// Check if overweight and gender to select correct life expectancy message
if (strcmp("male", gender)) {
if (bmi > 25) {
lifeExpect = 81.4;
} else {
lifeExpect = 83.5;
}
} else {
if (bmi > 25) {
lifeExpect = 77.3;
} else {
lifeExpect = 79.4;
}
}
// Display values and output
printf("For a %s with weight(kg) of %.1fkg and height(cm) of %.1fcm\n", gender, weight, height);
printf("You have a BMI of %.1f and a life expectancy of %.1f years\n", bmi, lifeExpect);
printf("You also have a BSA calculated using\n");
printf(" - Mosteller's:\t%.3f\n", mostellar);
printf(" - DuBois:\t\t%.3f\n", duBois);
printf(" - Boyd:\t\t%.3f\n", boyd);
return 0;
}