C Exercise Example 20 - Free Fall of a Ball

100 Classic C Examples

Title:A ball falls freely from a height of 100 meters. After each landing, it rebounds to half of its original height, then falls again. Find: by the time of the 10th landing, how many meters has it traveled in total? How high is the 10th rebound?

Program analysis:See notes below.

Program source code:

Example

#include <stdio.h> #define INITIAL_HEIGHT 100.0 #define BOUNCE_COUNT 10 int main() { double h = INITIAL_HEIGHT; double s = INITIAL_HEIGHT; // First rebound height h /= 2; // Calculate subsequent rebound heights and total distance for (int i = 2; i <= BOUNCE_COUNT; i++) { s += 2 * h; h /= 2; } // Output the result, specifying 2 decimal places printf("When landing for the %d time, the total distance traveled is %.5f meters, and the height of the %d rebound is %.5f meters.\n", BOUNCE_COUNT, s, BOUNCE_COUNT, h); return 0; }

The output of the above example is:

第10次落地时,共经过299.60938米,第10次反弹高0.09766米

100 Classic C Examples

other extensions