fork download
  1.  
  2. //Dione Torrevillas CS1A Chapter 6, P. 370, #6
  3. //
  4. /****************************************************************
  5.   *
  6.   * COMPUTE KINETIC ENERGY
  7.   *_______________________________________________________________
  8.   * This program computes the kinetic energy of a moving object
  9.   * based on its mass and velocity. The program uses a function
  10.   * to calculate and return the kinetic energy.
  11.   *
  12.   * Computation is based on the formula:
  13.   * Kinetic Energy = 0.5 * Mass * Velocity * Velocity
  14.   *_______________________________________________________________
  15.   * INPUT
  16.   * mass : Mass of the object in kilograms
  17.   * velocity : Velocity of the object in meters per second
  18.   *
  19.   * OUTPUT
  20.   * energy : Kinetic energy of the object in joules
  21.   *
  22.   ****************************************************************/
  23.  
  24. #include <iostream>
  25. #include <iomanip>
  26. using namespace std;
  27.  
  28. // Function prototype
  29. double kineticEnergy(double, double);
  30.  
  31. int main()
  32. {
  33. double mass; //INPUT - Mass in kilograms
  34. double velocity; //INPUT - Velocity in meters per second
  35. double energy; //OUTPUT - Kinetic energy in joules
  36.  
  37. // Get the mass of the object
  38. cout << "Enter the mass in kilograms: ";
  39. cin >> mass;
  40.  
  41. // Get the velocity of the object
  42. cout << "Enter the velocity in meters per second: ";
  43. cin >> velocity;
  44.  
  45. // Call the function to calculate kinetic energy
  46. energy = kineticEnergy(mass, velocity);
  47.  
  48. // Display the kinetic energy
  49. cout << fixed << setprecision(2);
  50. cout << "The kinetic energy is: "
  51. << energy << " joules" << endl;
  52.  
  53. return 0;
  54. }
  55.  
  56. // Function definition
  57. // Calculates and returns the kinetic energy
  58. double kineticEnergy(double mass, double velocity)
  59. {
  60. double energy; // Kinetic energy in joules
  61.  
  62. // Calculate the kinetic energy
  63. energy = 0.5 * mass * velocity * velocity;
  64.  
  65. return energy;
  66. }
  67.  
Success #stdin #stdout 0.01s 5312KB
stdin
Standard input is empty
stdout
Enter the mass in kilograms: Enter the velocity in meters per second: The kinetic energy is: 0.00 joules