Program to print the sequence 1-2+3-4+5-6+......n
Program to print the sequence 1-2+3-4+5-6+......n
#include<iostream>
using namespace std;
main()
{int n,sum=0;
cout<<"Enter number ";
cin>>n;
cout<<"The sequence is ";
for(int i=1;i<=n;i++)
{if(i%2==0)
{sum-=i;
cout<<"-"<<i; }
else
{
cout<<"+"<<i;
sum+=i;}
}
cout<<"\nAnd the sum is "<<sum;
}
Comments
Post a Comment