Code:
#include <stdio.h>
void getHeight(float *ft, float *in, float *meters, float sum) /*To get the user's height in feet and inches and convert it to meters*/
{
printf("Input your height in feet and inches respectively: ");
scanf("%f%f", &*ft, &*in);
sum = *ft + *in / 12;
*meters = sum * 0.3048;
}
void getWeight(float *lb, float *kg) /*To get the user's weight in pounds and convert it to kilograms*/
{
printf("Input your weight in pounds: ");
scanf("%f", &*lb);
*kg = *lb / 2.2;
}
void getBMI(float kg, float meters, float *bmi) /*To compute for the user's BMI(Body Mass Index) using his/her height and weight*/
{
*bmi = kg / (meters * meters);
}
void getGender(char *gender) /*To get the user's gender which will be used in computing for the ideal weight*/
{
printf("Are you (M)ale or (F)emale? ");
scanf(" %c", &*gender);
while(*gender != 'M' && *gender != 'F' && *gender != 'm' && *gender != 'f')
{
printf("Wrong input. Please input either M for Male or F for Female: ");
scanf(" %c", &*gender);
}
}
void getWeightCategory(float bmi, int *category) /*To get the user's weight category using the computed BMI*/
{
if(bmi < 18.5)
*category = 1;
else if(bmi >= 18.5 && bmi <= 24.9)
*category = 2;
else if(bmi >= 25 && bmi <= 29.9)
*category = 3;
else
*category = 4;
}
void getIdealWeight(int *ideal, float ft, float in, char gender, int category) /*To get the user's ideal weight*/
{
ft = ft * 12;
in = ft + in - 60;
if(gender == 'M' || gender == 'm')
{
*ideal = 105;
if(ft >= 60)
*ideal = 105 + in * 5;
}
else if(gender == 'F' || gender == 'f')
{
*ideal = 95;
if(ft >= 60)
*ideal = 95 + in * 4;
}
}
void getCaloriesNeeded(float pounds, int ideal, int *calneeded, int category)/*To get the calories needed to burn*/
{
if(category == 3 || category == 4)
*calneeded = (pounds - ideal) * 3500;
}
void getDays(int calneeded, int *days)/*To get the time needed to do the process*/
{
*days = calneeded / 1000;
printf("The process will take %d days\n", *days);
}
void displayResult(int category, int ideal, float bmi, int *gain, float pounds, int calneeded) /*To display the weight category, ideal weight, bmi, and calories needed to burn*/
{
printf("Your BMI(Body Mass Index) is %.1f.\n", bmi);
*gain = ideal - pounds;
if(category == 1)
printf("You are Underweight\nYou still need to exercise. Swimming, brisk walking, golf, or weight lifting should be good exercises for you.\nYour ideal weight is %d pounds.\nYou need to gain %d pounds.\n", ideal, *gain );
else if(category == 2)
printf("You are of Normal Weight\nKeep up what you are doing! Don't forget to exercise and eat healthy.\n");
else if(category == 3)
printf("You are Overweight\nYour ideal weight is %d pounds.\nYou need to burn %d calories to achieve desired weight.\n", ideal, calneeded);
else if(category == 4)
printf("You are Obese\nYour ideal weight is %d pounds.\nYou need to burn %d calories to achieve desired weight.\n", ideal, calneeded);
}
void getResponse(char *response) /*To know if the user wants the program to run again*/
{
printf("Would you like to run the program again? (Y)es or (N)o: ");
scanf(" %c", &*response);
while(*response != 'Y' && *response != 'N' && *response != 'y' && *response != 'n')
{
printf("Wrong input. Please input either Y for Yes or N for No: ");
scanf(" %c", &*response);
}
}
int main() /*Calls all the functions inside*/
{
/*Variables used to call the functions*/
int ideal, category, gain, calweight, calneeded, days, met1[12] = {12.3, 7.8, 7.2, 7.0, 6.8, 6.5, 6.0, 4.8, 4.3, 3.8, 2.3, 2.3}, met2[12] = {12.3, 7.8, 7.2, 7.0, 6.8, 6.5, 6.0, 4.8, 4.3, 3.8, 2.3, 2.3};
char gender, response;
float feet, inches, pounds, bmi, kg, meters, sum;
do
{
getHeight(&feet, &inches, &meters, sum);
getWeight(£s, &kg);
getGender(&gender);
getBMI(kg, meters, &bmi);
getWeightCategory(bmi, &category);
getIdealWeight(&ideal, feet, inches, gender, category);
getCaloriesNeeded(pounds, ideal, &calneeded, category);
displayResult(category, ideal, bmi, &gain, pounds, calneeded);
getDays(calneeded, &days);
getResponse(&response);
}
while(response == 'Y' || response == 'y'); /*The do-while statement inside int main() is used to ask if the user wants to run the program again (see function getResponse)*/
return 0;
}