WAP to compute the sum of the first n terms of the following series S =1-2+3-4+5…………….



#include<iostream.h>
#include<conio.h>
class summation
{
int  sum;
public:
void  s();
};
void  summation::s()
{
sum=0;
int n;
cout<<"enter the number of terms whose sum you want in the series"<<endl;
cin>>n;
                for(int i=1;i<=n;i++)
       {
       if(i%2!=0)
                sum=sum+i;
                else
                sum=sum-i;
       }
cout<<"the sum is :"<<sum;
}
void main()
{
clrscr();
summation obj;
obj.s();
getch();
}


Output:-


Comments