How to Get Sum of Series 2+4+6+8+…. +n in C Program

It is easy to get sum of series 2 + 4 + 6 + 8 + . . . + n in C program. In this tutorial, I’ll show you how to do it in easy way in C program. Here is a simple code for it:

#include <stdio.h>

int main()
{
int n,i;
int s=0;


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


printf("Sum of the series: ");


for (i =2; i <= n; i=i+2)
{
s=s+i;
if((i+2)>=n)
printf("%d = %d ",i,s);
else
printf("%d + ",i);
}


return 0;
}

Output:

Enter the value of n of the series: 10
Sum of the series: 2 + 4 + 6 + 8 + 10 = 30

In above code, I have tried to show number, plus sign, result and in a word equation  by iterating from 2 to n using for loop by incrementing 2 and as well as summing up the series in the iteration.

In this tutorial, I have shown how to get sum of series 2 + 4 + 6 + 8 + . . . + n in C Program. Hope you have enjoyed the tutorial. If you want to get updated, like the facebook page http://www.facebook.com/freetechtrainer and stay connected.

Add a Comment