g Write a complete C++ program that has the user enter the number of calories and the fat grams in a food. The program should calculate and display the percentage of calories that come from fat: One gram of fat has 9 calories Calories from fat = fat grams * 9 Percentage of calories from fat = calories from fat / total calories If the calories from fat are less than 30% of the total calories of the food, it should also display a message indicating the food is low in fat. Input validation—Number of calories and fat grams cannot be less than zero. Also, the number of calories from fat cannot be greater than the total number of calories. Display appropriate error messages. You do not need to check for cin failure. Sample test data: Calories Fat Output 100 3 grams 27 % fat, low in fat 200 12 grams 54% fat 50 0 0 % fat 90 -5 Error in input 45 8 Error in input

Respuesta :

Answer:

#include <iostream>

using namespace std;

int main()

{

   double calories, fat_grams, fat_calories;

   double percentage_of_calories;

   cout << "Enter the number of calories in a food: ";

   cin >> calories;

   cout << "Enter the number of fat grams in a food: ";

   cin >> fat_grams;

   

   if(calories >= 0 && fat_grams >= 0){

       fat_calories = fat_grams * 9;

       percentage_of_calories = (fat_calories / calories) * 100;

       

       if(fat_calories > calories) {

           cout << "Error in input" << endl;

       } else {

           cout << percentage_of_calories << "%" << " fat" << endl;  

       }

   

       if(percentage_of_calories < 0.30)

           cout << "Low in fat." << endl;

   } else {

       cout << "Error in input" << endl;

   }

   return 0;

}

Explanation:

- Declare variables

- Ask user to input calories and fat grams

- Check if calories and fat grams are greater than zero. If they are, calculate the fat calories and percentage of calories. Then, check if calories from fat is smaller than the total calories of the food. If it is, print the percentage. Also, check if the percentage is smaller than 30%.