YouTip LogoYouTip

C Exercise Example24

## C Programming Exercise - Example 24: Sum of a Fractional Sequence This tutorial demonstrates how to solve a classic mathematical sequence problem using C. We will analyze the pattern of a specific fractional sequence and write an efficient program to calculate the sum of its first 20 terms. --- ### Problem Description Given the following fractional sequence: $$\frac{2}{1}, \frac{3}{2}, \frac{5}{3}, \frac{8}{5}, \frac{13}{8}, \frac{21}{13}, \dots$$ Calculate the sum of the first **20 terms** of this sequence. --- ### Mathematical Analysis To solve this problem, we need to identify the mathematical relationship between the numerators and denominators of consecutive terms. Let's list the first few terms: * **Term 1:** $a_1 = \frac{2}{1}$ (Numerator = 2, Denominator = 1) * **Term 2:** $a_2 = \frac{3}{2}$ (Numerator = 3, Denominator = 2) * **Term 3:** $a_3 = \frac{5}{3}$ (Numerator = 5, Denominator = 3) * **Term 4:** $a_4 = \frac{8}{5}$ (Numerator = 8, Denominator = 5) #### The Pattern: 1. **Denominator of the next term** is equal to the **numerator of the current term**. 2. **Numerator of the next term** is equal to the **sum of the current term's numerator and denominator**. This pattern is closely related to the **Fibonacci sequence** ($1, 1, 2, 3, 5, 8, 13, 21, \dots$). If we let $a$ represent the numerator and $b$ represent the denominator: * Initial state: $a = 2$, $b = 1$ * Next state: * New Numerator ($a_{new}$) = $a + b$ * New Denominator ($b_{new}$) = $a$ (the old numerator) To implement this state transition in code, we must use a temporary variable `temp` to hold the value of $a$ before updating it. --- ### Code Implementation Below is the complete C program to solve this problem. ```c #include int main() { int i; float temp; float sum = 0.0; float a = 2.0, b = 1.0; // a is the numerator, b is the denominator for (i = 1; i <= 20; i++) { // Add the current term to the sum sum = sum + a / b; // Store the current numerator before updating it temp = a; // Update numerator and denominator for the next iteration a = a + b; // Next numerator is the sum of current numerator and denominator b = temp; // Next denominator is the current numerator } // Print the result with a width of 9 characters and 6 decimal places printf("The sum of the first 20 terms is: %9.6f\n", sum); return 0; } ``` --- ### Output When you compile and run the program, it produces the following output: ```text The sum of the first 20 terms is: 32.660263 ``` --- ### Key Considerations 1. **Data Types and Precision**: In C, integer division truncates the fractional part (e.g., `2 / 1` is `2`, but if both were integers, `3 / 2` would result in `1` instead of `1.5`). To prevent precision loss, we declare the numerator `a` and denominator `b` as `float` (or `double`). This ensures that floating-point division is performed. 2. **Temporary Variable**: The temporary variable `temp` is crucial. Without it, if you write `a = a + b;` first, the original value of `a` is lost, making it impossible to correctly assign the new value to `b`. 3. **Format Specifier `%9.6f`**: The format specifier `%9.6f` formats the output to be at least 9 characters wide (including the decimal point) with exactly 6 digits after the decimal point.
← C Exercise Example25C Exercise Example23 β†’