fork download
  1. //Cameron Pham CS1A Chapter 6, p. 370, #6
  2. //
  3. /******************************************************************************
  4. * CALCULATE KINETIC ENERGY
  5. * ______________________________________________________________________________
  6. * This program accepts floating point values for an object's mass and velocity
  7. * and computes the amount of kinetic energy the object has.
  8. * ______________________________________________________________________________
  9. * INPUT
  10. * Mass // Mass of an object in kilograms
  11. * Velocity // Velocity of an object in meters per second
  12. *
  13. * OUTPUT
  14. * Kinetic Energy // The object's kinetic energy calculated by KE = 1/2 * mv^2
  15. *
  16. *******************************************************************************/
  17. #include <iostream>
  18. using namespace std;
  19.  
  20. float kineticEnergy(float mass, float velocity);
  21.  
  22. int main()
  23. {
  24. // Initialize Variables
  25. float mass;
  26. float velocity;
  27. float ke;
  28.  
  29. // Ask the User for Object Mass and Velocity
  30. cout << "Enter your object's mass in kilograms and velocity in m/s.";
  31. ke = kineticEnergy(mass, velocity);
  32. cout << endl;
  33.  
  34. // Displaying Calculated Kinetic Energy
  35. cout << "Your object's kinetic energy is: " << ke << ".";
  36. return 0;
  37. }
  38.  
  39. float kineticEnergy(float mass, float velocity)
  40. {
  41.  
  42. float kineticEnergy;
  43.  
  44. // Receiving Input
  45. cin >> mass >> velocity;
  46. if(mass < 0)
  47. {
  48. cout << "Enter a value equal to 0 or greater.";
  49. }
  50.  
  51. // Computation for Kinetic Energy
  52. kineticEnergy = (mass * (velocity * velocity)) / 2;
  53.  
  54. return kineticEnergy;
  55.  
  56. }
Success #stdin #stdout 0s 5320KB
stdin
5 6.7
stdout
Enter your object's mass in kilograms and velocity in m/s.
Your object's kinetic energy is: 112.225.