# C Program to Display Sum of Some Series

There are different types of series in mathematics which can be solved easily in C programming. This section deals with finding sum of following types of series in C program.

- 1, 2, 5, 10, 17 … up to nth terms
- 1 + (1+2) + (1+2+3) + …
- 1
^{2}+2^{2}/3, 2^{2}+3^{2}/4, 3^{2}+4^{2}/5, ...

### 1. 1, 2, 5, 10, 17 … up to nth terms

To find the sum of the above series, observe two terms and try to find the difference between them. In this series, the difference between first two terms is 1. The difference between second and third term is 3 and the difference between third and fourth term is 5. So from this observation we come to know that the difference is increasing by odd number.

So, the program to find the sum of given series is given below.

#include<stdio.h> int main() { int s=1, n, d=1, i=1, ans=0; printf("Enter number of terms\n"); scanf ("%d",&n); printf("\n The series is: \n"); do { printf("%d \t",s); ans = ans+s; s = s + d; d = d + 2; i = i + 1; } while (i<=n); printf ("\n The sum of the series is %d", ans); return 0; }

Here, the variable *s* is the first term of the series, *n* is the number of terms of the series the user wants to find the sum of, *d* is the difference between two terms, *i* is a counter for loop and *ans* is the sum of the series.

In the first loop, the first term of the series is displayed at first. Then the term is added to *ans* (which is initialized to 0 to avoid addition of garbage values). The next term of the series is found by adding the first term (i.e. s) with the difference between the first and second term (i.e. d) so s = s + d gives the next term of the series. The difference in this series increases by odd number so d = d + 2 results in 3 which is the difference for the next two terms. Then the value of* i* is incremented by 1 as it acts as a counter. The loop continues as long as *i* is less than or equal to *n*.

**Output**

Enter number of terms 6 The series is: 1 2 5 10 17 26 The sum of the series is 61

### 2. 1 + (1+2) + (1+2+3) + …

In this series,

First term = 1

Second term = (1+2)

Third Term = (1+2+3)

So from these terms we can see that if

1^{st} term = 1

2^{nd} term = 1^{st} term + 1

3^{rd} term = 2^{nd} term + 2

4^{th} term = 3^{rd} term + 3 and so on.

So the program to calculate the sum of this series is given below:

#include<stdio.h> int main() { int b=2, ans=0, n, i=1, s=1; printf("Enter number of terms\n"); scanf ("%d",&n); printf("\n The series is: \n"); do { printf("%d \t",s); ans = ans+s; s = s+b; b = b+1; i = i + 1; } while (i<=n); printf ("\n The sum of the series is %d", ans); return 0; }

Here, *b* is the additional digit in the next term of the sequence (i.e. if first term is 1 then b=2 is the digit that is added for the second term), *ans* is the sum of the series which is initialized to 0 to avoid addition of garbage values, *n* is the number of terms whose sum is the be calculated, *i* is the counter for loop and *s* is the first term of the sequence.

In the first loop, at first, the first term is displayed. Then ans = ans+s adds first term to the sum of the series. The expressions s=s+b generates the next term (where b in first loop is 2 so s= s+b=1+2=3 which is the 2^{nd} term). b is the additional digit for the next term so it is increased by 1. The loop continues as long as i is less than or equal to n.

**Output**

Enter number of terms 4 The series is: 1 3 6 10 The sum of the series is 20

### 3. 1^{2}+2^{2}/3, 2^{2}+3^{2}/4, 3^{2}+4^{2}/5, ...

In this series,

First term = 1^{2}+2^{2}/3

Second term = 2^{2}+3^{2}/4

Third term = 3^{2}+4^{2}/5

From these terms we can see that the difference between the each of the terms is that each digit (base not power) is increased by 1 than in the previous term. So the program to calculate the sum of this series is given below.

#include<stdio.h> #include<math.h> int main() { float a=1, b=2, c=3, ans=0, n, i=1, x, y; printf("Enter number of terms \n"); scanf ("%f",&n); printf("\n The series is: \n"); do { x = pow(a,2); y = pow(b,2); printf("%f + %f / %f \n",x,y,c); ans = ans + x + y/c; a = a+1; b = b+1; c = c+1; i = i + 1; } while (i<=n); printf ("\n The sum of the series is %f", ans); return 0; }

Here, *a*, *b*, *c* are the three digits in a term which is initialized to 1, 2 and 3 respectively and *ans* is the sum of the series, *n* is the number of terms in the series, *i* is used as counter in loop, *x* and *y* store the square of *a* and *b* respectively.

In the first loop, *x* and *y* stores the squared values of *a* and *b*. Then the first term of series is displayed. The expression ans = ans + x + y/c adds the value of the 1^{st} term to the variable ans(which is at first initialized to 0). Then the value of *a*, *b* and *c* are increased for the next loop. The counter *i* is also increased. The loop goes on as long as the value of *i* is less than or equal to *n*.

**Output**

Enter number of terms 3 The series is: 1 + 4 / 3 4 + 9 / 4 9 + 16 / 5 The sum of the series is 20.78