Find Print Sum Of Series 1 – 3 + 5 … N in C program

Find Print Sum Of Series 1 – 3 + 5… n in C program


Here I shared C program to print sum of series 1 – 3 + 5 ... N. Given an integer N, the task is to print the total of first N terms of the series:


What is the sum of 1-3+5…N?

Below C program has given you the best way to find the sum of series.

C Program 



#include<stdio.h>
int main()
{
int i,m=1,n,sum=0;
printf("\nEnter the number of terms");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(i<n)
{
if(i%2==0)
{
printf("%d+",m);
sum=sum-m;
}
else
{
printf("%d-",m);
sum=sum+m;
}
}
else
{
printf("%d=",m);
if(n%2==0)
{
sum=sum-m;
}
else
{
sum=sum+m;
}
}
m=m+2;
}
printf("%d",sum);
return 0;
}



OUTPUT


Enter the number of terms5
1-3+5-7+9=5

In the Above, C program find print the sum of series 1 – 3 + 5 … N and if you have any doubt please comment below.


Share this

0 Comment to "Find Print Sum Of Series 1 – 3 + 5 … N in C program"

Post a Comment