fork download
  1. // =============================================================
  2. //Nicolas Ruano CS1A Chapter 5, P. 294, #4
  3. //
  4. /**************************************************************
  5. *
  6. * COMPUTES AND DISPLAYS CALORIES BURNED AT 3.9 Cal/min CONSTANT.
  7. * ____________________________________________________________
  8. * This program computes and displays calories burned on a
  9. * treadmill at a constant rate of 3.9 calories per minute. After
  10. * the user enters a non-negative speed, it shows total calories
  11. * burned after 10, 15, 20, 25, and 30 minutes. If a negative
  12. * speed is entered, the user is prompted again until a valid
  13. * value is given.
  14. *
  15. * Computation is based on the formula:
  16. * Calories Burned = Time in Minutes×3.9Calories Burned.
  17. * ____________________________________________________________
  18. * INPUT
  19. * speed
  20. *
  21. *
  22. * OUTPUT
  23. * minutes
  24. * caloriesBurned
  25. *
  26. **************************************************************/
  27.  
  28. #include <iostream> // For input and output
  29.  
  30. using namespace std;
  31.  
  32. int main() {
  33. // --- Step 1: Declare variables ---
  34. double speed; // User's running speed
  35. const double CALORIES_PER_MINUTE = 3.9; // Fixed rate
  36. int times[] = {10, 15, 20, 25, 30}; // Times to calculate
  37. int numberOfTimes = 5; // Total time entries
  38.  
  39. // --- Step 2: Ask for speed ---
  40. cout << "Enter your running speed: ";
  41. cin >> speed;
  42.  
  43. // --- Step 3: Input validation ---
  44. while (speed < 0) {
  45. cout << "Speed must be zero or positive. Please enter again: ";
  46. cin >> speed;
  47. }
  48.  
  49. // --- Step 4: Loop through the time values ---
  50. for (int i = 0; i < numberOfTimes; i++) {
  51. int minutes = times[i]; // Current time from the list
  52. double caloriesBurned = minutes * CALORIES_PER_MINUTE;
  53. cout << "After " << minutes << " minutes: "
  54. << caloriesBurned << " calories burned." << endl;
  55. }
  56.  
  57. // --- Step 5: End the program ---
  58. return 0;
  59. }
Success #stdin #stdout 0s 5324KB
stdin
Standard input is empty
stdout
Enter your running speed: After 10 minutes: 39 calories burned.
After 15 minutes: 58.5 calories burned.
After 20 minutes: 78 calories burned.
After 25 minutes: 97.5 calories burned.
After 30 minutes: 117 calories burned.