fork download
  1. //Xinnuo Wang CS1A chapter 4, p.223 #16
  2. //
  3. /*******************************************************************************
  4.  *
  5.  * Caculate Fat Grams
  6.  * _____________________________________________________________________________
  7.  * This program accepts the number of calories and fat grams and caculates the
  8.  * percentage of calories that come from fat
  9.  *
  10.  * Computation is based on the formula:
  11.  * Calories from fat = fat grams * 9
  12.  * The percentage of calories = Calories from fat ÷ total calories
  13.  * _____________________________________________________________________________
  14.  * INPUT
  15.  * Total calories : The accepted number of total calories
  16.  * Total grams : The accepted number of grams from food
  17.  * PROCESS
  18.  * Calories from fat : The number of calories from fat
  19.  * OUTPUT
  20.  * The percentage of calories from fat : The percentage of calories from fat
  21.  *
  22.  ******************************************************************************/
  23. #include <iostream>
  24. using namespace std;
  25.  
  26. int main()
  27. {
  28. double totalGrams; //INPUT - The accepted number of total calories
  29. double totalCalories; //INPUT - The accepted number of total grams
  30. double caloriesFromFat; //PROCESS - The number of calories from fat
  31. double percentageOfCaloriesFromFat; //OUTPUT - The percentage of calories from fat
  32. //
  33. //Data Input
  34. cout << "Enter the number of grams" << endl;
  35. cin>> totalGrams;
  36. cout << "Enter the number of calories" << endl;
  37. cin>> totalCalories;
  38. //
  39. //Caculate Calories from Fat
  40. caloriesFromFat = totalGrams * 9;
  41. //
  42. //Caculate The Percentage of Calories from Fat
  43. percentageOfCaloriesFromFat = caloriesFromFat / totalCalories * 100;
  44. //Output
  45. if(percentageOfCaloriesFromFat< 30)
  46. cout << "The food is low in fat" << endl;
  47. else if(totalGrams<0 && totalCalories< 0)
  48. cout<< "Invalid input" <<endl;
  49. else if(caloriesFromFat>totalCalories)
  50. cout << "either the calories or fat grams were incorrectly entered" <<endl;
  51. return 0;
  52. }
Success #stdin #stdout 0s 5324KB
stdin
4.6 200
stdout
Enter the number of grams
Enter the number of calories
The food is low in fat