# Sum of Series Program in C

In this tutorial we will write sum of series program in C language. Some series as 1+2+3+4+5+…+n, 1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6 +……+ 1/n and 1 + 1/(2*2) + 1/(3*3) + 1/(4*4) + ….. + 1/(n*n)

## Sum of series in C 1+2+3+4+5+..+N

Program Description:- Display sum of series 1+2+3+4+5+..+n = ?

Sample Input Output-1:-

Enter n value: 5
1+2+3+4+5=15

Sample Input Output-2:-

Enter n value: 15
1+2+3+4+5+6+7+8+9+10+11+12+13+14+15=120

``````#include<stdio.h>
int main()
{
int n, sum=0;

printf("Enter n value: ");
scanf("%d",&n);

for(int i=1; i<=n; i++)
{
printf("%d+",i);
sum += i; //sum = sum + i;
}

printf("\b=%d",sum);

return 0;
}``````

In this program ‘\b’ character used to remove last ‘+’ character. If we don’t use ‘\b’ then for n=5 we will get below output,

Enter n value: 5
1+2+3+4+5+=15

Enter n value: 10
1+2+3+4+5+6+7+8+9+10=55

## Sum of series in C language 1² + 2² + 3² + 4² + 5² +….+ n²

Program description:- Find sum of series 1² + 2² + 3² + 4² + 5² +….+ n² = ?

Sample Input Output 1

Enter n value: 5
Sum = 55

Sample Input Output 2

Enter n value: 12
Sum=650

``````#include<stdio.h>
int main()
{
int n, sum=0;

printf("Enter n value: ");
scanf("%d",&n);

for(int i=1; i<=n; i++)
{
sum += i*i; //sum = sum + (i*i);
}

printf("Sum=%d",sum);

return 0;
}``````

Enter n value: 7
Sum = 140

## Sum of series 1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6 +……+ 1/n

Using For Loop

``````#include<stdio.h>
int main()
{
int n,i;
float sum=0.0;

printf("Enter the limit (n Value): ");
scanf("%d",&n);

for(i=1;i<=n;i++)
{
sum+=(1.0/i); //sum = sum + 1.0/i
}

printf("Sum = %f\n", sum);

return 0;
}``````

1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6 +……+ 1/n using While Loop

``````#include<stdio.h>
int main()
{
int n, i=1;
float sum=0.0;

printf("Enter the limit (n Value): ");
scanf("%d",&n);

while(i<=n)
{
sum+=(1.0/i); //sum = sum + 1.0/i
i++;
}

printf("Sum = %f\n", sum);

return 0;
}``````

Output:-

Enter the limit (n Value): 50
Sum = 4.499206

## 1 + 1/(2*2) + 1/(3*3) + 1/(4*4) + ….. + 1/(n*n)

Using For Loop

``````#include<stdio.h>
int main()
{
int n,i;
float sum=0.0;

printf("Enter the limit (n Value): ");
scanf("%d",&n);

for(i=1;i<=n;i++)
{
sum+=(1.0/(i*i)); // sum = sum + (1.0 / (i*i) )
}

printf("Sum = %f", sum);
return 0;
}``````

1 + 1/(2*2) + 1/(3*3) + 1/(4*4) + ….. + 1/(n*n) using While Loop

``````#include<stdio.h>
int main()
{
int n, i=1;
float sum=0.0;

printf("Enter the limit (n Value): ");
scanf("%d",&n);

while(i<=n)
{
sum+=(1.0/(i*i)); // sum = sum + (1.0 / (i*i) )
i++;
}

printf("Sum = %f\n", sum);

return 0;
}``````

Sum of series in C language 1 + 1/(2*2) + 1/(3*3) + 1/(4*4) + ….. + 1/(n*n) using pow() Function

Using pow() function, we can use either while loop or for loop. Here, while loop is used. You can also use for loop instead of while loop.

``````#include<stdio.h>
#include<math.h>
int main()
{
int n, i=1;
float sum=0.0;

printf("Enter the limit (n Value): ");
scanf("%d",&n);

while(i<=n)
{
sum+=(1.0/pow(i,2)); //sum = sum + 1.0/ pow(i,2)
i++;
}

printf("Sum = %f", sum);

return 0;
}``````

Output:-

Enter the limit (n Value): 50
Sum = 1.625133

In this sum of series in C language program, If you are using Linux/Unix OS and getting an error then read it “undefined reference to sqrt (or other mathematical functions)” even include math.h header

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!