Q-04: Write a C/C++ program to compute the sum of the first n terms of the series.
S = 1 - 2 + 3 - 4 + 5 . . . n
    

Using - C


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

OUTPUT

Enter the number of terms: 7
Sum of the Series: 4
        

Using - C++


#include<iostream> using namespace std; int main() { int n, sum=0; cout<<"Enter the number of terms: "; cin>> n; for(int i=1; i<=n; i++){ if(i%2==0) sum -= i; else sum += i; } cout<<"Sum of the Series: "<< sum; return 0; }

OUTPUT

Enter the number of terms: 4
Sum of the Series: -2