fork download
  1. #include <iostream> // For cin and cout
  2. #include <iomanip> // For setprecision and formatting
  3.  
  4. using namespace std;
  5.  
  6. // Constant for acceleration due to gravity (in meters per second squared)
  7. const double GRAVITY = 9.8;
  8.  
  9. /**
  10.  * @brief Calculates the falling distance of an object given time.
  11.  *
  12.  * Formula: distance = 0.5 * g * t^2
  13.  *
  14.  * @param time Time in seconds
  15.  * @return Distance in meters
  16.  */
  17. double fallingDistance(double time) {
  18. return 0.5 * GRAVITY * time * time;
  19. }
  20.  
  21. int main() {
  22. // Set the output to display fixed-point notation with 2 decimal places
  23. cout << fixed << setprecision(2);
  24.  
  25. cout << "Time (s)\tDistance Fallen (m)\n";
  26. cout << "-------------------------------\n";
  27.  
  28. // Loop from 1 to 10 seconds
  29. for (int time = 1; time <= 10; ++time) {
  30. double distance = fallingDistance(time);
  31. cout << time << "\t\t" << distance << endl;
  32. }
  33.  
  34. return 0;
  35. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Time (s)	Distance Fallen (m)
-------------------------------
1		4.90
2		19.60
3		44.10
4		78.40
5		122.50
6		176.40
7		240.10
8		313.60
9		396.90
10		490.00