The program must accept an integer N as the input. The program must print the sum of the first N terms in the series given below.
The order of the series must be 1, -2, 3, -4, 5, -6 and so on (All the odd terms in the series formed from the positive odd numbers and all the even terms in the series formed from the negative even numbers).Boundary Condition(s):
1 <= N <= 10^4
Input Format:
The first line contains the integer N.
Output Format:
The first line contains the sum of the first N terms in the above mentioned series.
Example Input/Output 1:
Input:
5
Output:
3
Explanation:
The first 5 terms are 1, -2, 3, -4 and 5 and their sum is 3 (1 - 2 + 3 - 4 + 5).
Hence the output is 3
Program
#include<stdio.h>
#include <stdlib.h>
int main()
{
int sum=0,i,j,N;
scanf("%d",&N);
for(i=1;i<=N;i+=1)
{
if (i%2==0)
sum=sum + (-1)*i;
else
sum=sum+i;
}
printf("%d",sum);
}
Output:
Input:
8
Output:
-4
No comments:
Post a Comment