fork download
  1. //Kanoa Holden CS1A Chapter 3, P. 144 Q#7
  2. //
  3. /*******************************************************************************
  4.  *
  5.  * COMPUTE CALORIES ATE FROM NUMBER OF COOKIES
  6.  * _____________________________________________________________________________
  7.  * This program calculates the calories consumed based on the number of cookies
  8.  * ate.
  9.  * _____________________________________________________________________________
  10.  * INPUT
  11.  * total_cookies : total amount of cookies ate
  12.  * servings : serving size of cookies
  13.  * calories_per_serving : the amount of calories per serving of cookies
  14.  *
  15.  * OUTPUT
  16.  * total_calories : total amount of calories consumed
  17.  *
  18.  ******************************************************************************/
  19. #include <iostream>
  20.  
  21. using namespace std;
  22.  
  23. int main() {
  24. // Constants for total cookies, servings, and calories per serving
  25. const int total_cookies = 40;
  26. const int servings = 10;
  27. const int calories_per_serving = 300;
  28.  
  29. // Calculate how many cookies are in each serving
  30. int cookies_per_serving = total_cookies / servings;
  31.  
  32. // Ask the user how many cookies they ate
  33. int cookies_eaten;
  34. cout << "\nEnter the number of cookies you ate: ";
  35. cin >> cookies_eaten;
  36.  
  37. // Calculate how many calories were consumed
  38. double calories_per_cookie = static_cast<double>(calories_per_serving) / cookies_per_serving;
  39. double total_calories = cookies_eaten * calories_per_cookie;
  40.  
  41. // Display the total calories consumed
  42. cout << "\nYou consumed " << total_calories << " calories." << endl;
  43.  
  44. return 0;
  45. }
Success #stdin #stdout 0s 5276KB
stdin
6
stdout
Enter the number of cookies you ate: 
You consumed 450 calories.