C++ Fat Gram Calculator Write a program that asks for the number of calories and
ID: 3805595 • Letter: C
Question
C++ Fat Gram Calculator Write a program that asks for the number of calories and fat grams in a food. The program should display the percentage of calories that come from fat. If the calories from fat are less than 30% of the total calories of the food, it should also display a message indicating that the food is low in fat. One gram of fat has 9 calories, so Calories from fat = fat grams * 9 The percentage of calories from fat can be calculated as Calories from fat total calories Input Validation: Make sure the number of calories and fat grams are not less than 0. Also, the number of calories from fat cannot be greater than the total number of calories. If that happens, display an error message indicating that either the calories or fat grams were incorrectly entered.Explanation / Answer
Here goes the required code for the given problem. Given conditions are included and appropriate error messages are displayed.
Code:
#include <iostream>
using namespace std;
int main() {
// your code goes here
double c,f;
cout <<"Enter the number of calories and fat grams " ;
cin>>c>>f;
double fc=f*9;
double per=fc/c;
if(c<fc){
cout<<"either the calories or fat is incorrect";
}else if(c<0 || f<0){
cout<<"either the calories or fat is incorrect";
}else if(per<0.3){
cout<<"food is low in fat";
}else{
cout<<"food is high on fat";
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.