Series of Fractions in C: 1/2 + 2/3 + 3/4
In this article, we will explore how to calculate the sum of a series of fractions in C programming language. Specifically, we will focus on the series 1/2 + 2/3 + 3/4.
Problem Statement
Write a C program to calculate the sum of the series 1/2 + 2/3 + 3/4.
Solution
To solve this problem, we can use a simple C program that calculates the sum of the series. Here is the code:
#include
int main() {
float sum = 0.0;
float denom = 2.0;
float numer = 1.0;
for (int i = 0; i < 3; i++) {
sum += numer / denom;
numer++;
denom++;
}
printf("The sum of the series is: %f\n", sum);
return 0;
}
Explanation
In this program, we use a for
loop to iterate three times, since we have three terms in the series. We initialize a sum
variable to store the sum of the series and two variables, denom
and numer
, to represent the denominator and numerator of each term, respectively.
In each iteration, we calculate the fraction by dividing the numerator by the denominator and add it to the sum
. We then increment the numerator and denominator by 1 for the next iteration.
Finally, we print the sum of the series using printf
.
Output
When we run the program, the output will be:
The sum of the series is: 2.416667
Conclusion
In this article, we have successfully calculated the sum of the series 1/2 + 2/3 + 3/4 using a simple C program. This demonstrates the basic concept of iterating over a series and calculating the sum of its terms.