Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

john raymond was so pleased with the program you created for his clients that he

ID: 3534173 • Letter: J

Question

john raymond was so pleased with the program you created for his clients that he regers you to his coworker,a nutrionist in the same facility.The nutrionist would like you to create a program that calculates fat grams and calories.Design a program that asks for the number of fat grams and calories in a food item.Validate the input as follows:
-make sure the number of fat grams and calories are not less then 0.
-according to nutritional formulas,the number of calories cannot exceed fat grams x 9 . Make sure that the number of calories entered is not greater than fat grams x9.
Once correct data has been entered the program should calculate and display the percentage of calories that come from fat.Use the following formula: percentage of calories from fat=(fat grams x 9) / calories
some nutritionist classify a food as low fat if less than 30 percent of its calories come from fat.if the results of this fomrula are less then 0.3 the program should display a message indicating the food is low in fat

Explanation / Answer

The following is written as a C function but could be easily modified to work in another language. The function returns -1 if input validation fails, or the percentage of calories from fat if it runs successfully.

double fatGramCalculator(double fatGrams, double calories)
{

// If fat grams or calories are less than 0, print a message and return -1
if (fatGrams < 0 || calories < 0)
{
printf("Number of fat grams and calories must be greater than 0 ");
return -1;
}

// If calories is greater than fat grams * 90, print a message and return -1
if (calories > (fatGrams * 9)
{
printf("Calories must not exceed fat grams x 9 ");
return -1;
}

// Calculate percentage of calories from fat
double caloriesFromFat = (fatGrams * 9) / calories;

// Print calories from fat, multiplying percentage by 100 to remove decimal point
printf("%d percent of calories from fat ", caloriesFromFat * 100);

// If less than 30% of calories come from fat, print a message
if (caloriesFromFat < 0.3)
{
printf("Food is low in fat ");
}

// Return the number of calories from fat
return caloriesFromFat;
}