fork download
  1. //Charlotte Davies-Kiernan CS1A Chapter 6 P. 370 #6
  2. //
  3. /*****************************************************************************
  4.  *
  5.  * Compute Kinetic Energy
  6.  * ___________________________________________________________________________
  7.  * This program will compute the kinetic energy of an object using the mass
  8.  * and velocity entered
  9.  *
  10.  * Formula used:
  11.  * kinetic energy = mass * velocity * velocity * 0.5
  12.  * ___________________________________________________________________________
  13.  * Input
  14.  * mass //Mass of the object
  15.  * velocity //Velocity of the object
  16.  * Output
  17.  * kE //Kinetic Energy of the object
  18.  *****************************************************************************/
  19. #include <iostream>
  20. #include <iomanip>
  21. using namespace std;
  22.  
  23. //Function Prototype
  24. float kineticEnergy(float mass, float velocity);
  25.  
  26. int main() {
  27. //Data Dictionary
  28. float mass; //INPUT - Mass of the object
  29. float velocity; //INPUT - Velocity of the object
  30. float kE; //OUPUT - Kinetic Energy of the object
  31. cout << "Kinetic Energy Calculator: " << endl;
  32. //Get User's Inputs
  33. cout << "Enter the object's mass (in kilograms): " << endl;
  34. cin >> mass;
  35. //Input Validation
  36. while (mass < 0){
  37. cout << "Mass cannot be negative. Please enter a positive value: " << endl;
  38. cin >> mass;
  39. }
  40. cout << "Enter the object's velocity (in meters per second): " << endl;
  41. cin >> velocity;
  42. //Input Validation
  43. while (velocity < 0){
  44. cout << "Velocity cannot be negative. Please enter a positive value: " << endl;
  45. cin >> velocity;
  46. }
  47. //Calculate Kinetic Energy
  48. kE = kineticEnergy(mass, velocity);
  49.  
  50. //Display Result
  51. cout << fixed << setprecision(2);
  52. cout << "The object's kinetic energy is " << kE << " joules" << endl;
  53. return 0;
  54. }
  55. //Kinetic Energy Function
  56. float kineticEnergy(float mass, float velocity){
  57. return mass * velocity * velocity * 0.5;
  58. }
Success #stdin #stdout 0.01s 5308KB
stdin
34
5
stdout
Kinetic Energy Calculator: 
Enter the object's mass (in kilograms): 
Enter the object's velocity (in meters per second): 
The object's kinetic energy is 425.00 joules