fork download
  1. //Zachary Abdollahi CS1A Chapter 6, P. 370, #6
  2. //
  3. /*******************************************************************************
  4.  *
  5.  * COMPUTE KINETIC ENERGY
  6.  *
  7.  * This program computes the kinetic energy of an object based on its mass
  8.  * (in kilograms) and velocity (in meters per second).
  9.  *
  10.  * Computation is based on the formula:
  11.  * KE = 0.5 x mass x velocity^2
  12.  *
  13.  * INPUT
  14.  * mass : Object's mass in kilograms
  15.  * velocity : Object's velocity in meters per second
  16.  *
  17.  * OUTPUT
  18.  * kineticEnergy : Amount of kinetic energy in Joules
  19.  *
  20.  ******************************************************************************/
  21.  
  22. #include <iostream>
  23. #include <iomanip>
  24. #include <cmath> // Needed for pow function
  25. using namespace std;
  26.  
  27. // Function Prototypes
  28. double kineticEnergy(double mass, double velocity);
  29.  
  30. int main()
  31. {
  32. float mass; // To hold the object's mass
  33. float velocity; // To hold the object's velocity
  34. float ke; // To hold the kinetic energy result
  35.  
  36. // Get input from the user
  37. cout << "Enter the object's mass (in kilograms): ";
  38. cin >> mass;
  39.  
  40. cout << "Enter the object's velocity (in meters per second): ";
  41. cin >> velocity;
  42.  
  43. // Call the function to calculate kinetic energy
  44. ke = kineticEnergy(mass, velocity);
  45.  
  46. // Display the formatted result
  47. cout << fixed << showpoint << setprecision(2);
  48. cout << "\nThe object's kinetic energy is " << ke << " Joules. " << endl;
  49.  
  50. return 0;
  51. }
  52.  
  53. /******************************************************************************
  54.   * kineticEnergy *
  55.   * Accepts mass (kg) and velocity (m/s) as arguments and calculates *
  56.   * and returns kinetic energy using KE = 0.5 * m * v^2. *
  57.   *****************************************************************************/
  58. double kineticEnergy(double mass, double velocity)
  59. {
  60. return 0.5 * mass * pow(velocity, 2.0);
  61. }
Success #stdin #stdout 0s 5320KB
stdin
10
5
stdout
Enter the object's mass (in kilograms): Enter the object's velocity (in meters per second): 
The object's kinetic energy is 125.00 Joules.