fork download
  1. //Nicolas Ruano CS1A Chapter 6, P. 370, #6
  2. /**************************************************************
  3. * FUNCTION COMPUTE THE MOVING OBJECT’S KINETIC ENERGY.
  4.  
  5. * _____________________________________________________________
  6.  
  7. * The program uses user-inputted mass and velocity to calculate
  8. * kinetic energy functions “kineticEnergy”, then displays the result.
  9. *
  10. * Computation is based on the formula:
  11. *
  12. * KE = 1/2 * m * v^2
  13.  
  14. * Where:
  15. * KE = Kinetic energy in Joules
  16. * m = Mass in kilograms
  17. * v = Velocity in meters per second
  18. * ____________________________________________________________
  19.  
  20. * INPUT
  21. * mass double Mass of the object in kilograms
  22. * velocity double of the object in m/s
  23. *
  24. * OUTPUT
  25. * mass double Mass of the object in kilograms
  26. * velocity double of the object in m/s
  27. * KE double Calculated kinetic energy in Joules
  28. *
  29. **************************************************************/
  30. #include <iostream>
  31. using namespace std;
  32.  
  33. // Function to calculate kinetic energy
  34. double kineticEnergy(double mass, double velocity) {
  35. double KE = 0.5 * mass * velocity * velocity; // KE = 1/2 * m * v^2
  36. return KE;
  37. }
  38.  
  39. int main() {
  40. double mass, velocity;
  41.  
  42. // Ask the user for mass and velocity
  43. cout << "Enter the mass of the object (in kilograms): ";
  44. cin >> mass;
  45. cout << "Enter the velocity of the object (in meters per second): ";
  46. cin >> velocity;
  47.  
  48. // Call the function and display the kinetic energy
  49. double KE = kineticEnergy(mass, velocity);
  50. cout << "The kinetic energy of the object is: " << KE << " Joules" << endl;
  51.  
  52. return 0;
  53.  
  54. }
Success #stdin #stdout 0s 5316KB
stdin
Standard input is empty
stdout
Enter the mass of the object (in kilograms): Enter the velocity of the object (in meters per second): The kinetic energy of the object is: 0 Joules