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