Hello all,

I have done with this code

Output :
Enter n = 5
2+4+6+8+10 = 30

I compile with Turbo C++, version 3
here is my code

#include <iostream.h>
#include <conio.h>
void main() {
       clrscr();
       int n, sum=0;
       cout<<"Enter n= "; cin>>n;
       for (int i=1; i<=n; i++) {
       cout<<2*i<<"+";
       sum = sum + i;      
       }
       cout<<"\b\b ="<<sum;
       getch();
}

--------------------------

By the way, could you help me if I want to have output like this

Enter n =5
2-4+6-8+10 = ....

Thanks in advance,

>#include <iostream.h>
I'll let that slide because you're using a compiler that's roughly the same age as dirt.

>#include <conio.h>
This I won't let slide. You have no reason to use conio, and I'll explain why shortly.

>void main() {
I don't care if the compiler lets you do it, void main is wrong and it always has been. The only two correct ways to define main in C++ are as follows:

int main()
{
  return 0; // With a newer compiler you can omit this
}
int main ( int argc, char *argv[] )
{
  return 0; // With a newer compiler you can omit this
}

>clrscr();
1) This is completely unnecessary.
2) It's completely anti-social because you lose the output of previous programs.
3) It destroys any portability your code might have had.
4) The cool kids don't use it, and you want to be cool, right?

>sum = sum + i;
I suppose you meant this instead:

sum = sum + ( 2 * i );

>cout<<"\b\b ="<<sum;
Those backspaces are messing up your output, I don't see why you even need them.

>getch();
1) This is completely unnecessary.
2) It's easy to get similar behavior with getchar.
3) It destroys any portability your code might have had.
4) The cool kids don't use it, and you want to be cool, right?

Comments
the "cool kids don't use it" lol

Hint: (untested)

#include <iostream.h>
#include <conio.h>
void main() {
       clrscr();
       int n, sum=0;
       cout<<"Enter n= "; cin>>n;
       for (int i=1; i<=n; i++) {
       int p =2*i;
       cout << p;
       if (i%2==0){
       
       sum=sum- p;  
        cout << "-";    
       }
       else{
         sum=sum+p;
         if (i!=n)
         {cout<<"+";}}}
         cout<<"="

       cout<<sum<<endl;
       getch();      
}

Hint: (untested)

#include <iostream.h>
#include <conio.h>
void main() {
       clrscr();
       int n, sum=0;
       cout<<"Enter n= "; cin>>n;
       for (int i=1; i<=n; i++) {
       int p =2*i;
       cout << p;
       if (i%2==0){
       
       sum=sum- p;  
        cout << "-";    
       }
       else{
         sum=sum+p;
         if (i!=n)
         {cout<<"+";}}}
         cout<<"="

       cout<<sum<<endl;
       getch();      
}

Hello,

Thanks for your help, It is ok now.

just looking through that you might need to change the couts:

cout << "-"; //"+"
cout << "+"; //"-"

I think they're mixed up. And maybe throw another
if (i!=n) in there too for good measure.

Hello all,

I have done with this code

Output :
Enter n = 5
2+4+6+8+10 = 30

I compile with Turbo C++, version 3
here is my code

#include <iostream.h>
#include <conio.h>
void main() {
       clrscr();
       int n, sum=0;
       cout<<"Enter n= "; cin>>n;
       for (int i=1; i<=n; i++) {
       cout<<2*i<<"+";
       sum = sum + i;      
       }
       cout<<"\b\b ="<<sum;
       getch();
}

--------------------------

By the way, could you help me if I want to have output like this

Enter n =5
2-4+6-8+10 = ....

Thanks in advance,

Here is the program
#include <iostream.h>
#include <conio.h>
#include <math.h>
void main() {
clrscr();
int n, sum=0,s1=0,s=0;
cout<<"Enter n= "; cin>>n;
for (int i=1; i<=n; i++) {
s1 =i*2;


s = pow(-1,i+1);
s1 = s1 *s;
cout<<s1;
if(s1<0)

cout<<"+";
sum = sum +s1;

}
cout<<"="<<sum;
getch();
}

Here at DaniWeb, we perfer to:
1) use code tags when posting code
2) read the rules so you know how to post
3) not give ready-made programs but help people learn how to program
4) post good code when helping, not bad code, like:
getch(), void main(), proper formatting, and proper headers (.h-less headers rather than old ones)

This article has been dead for over six months. Start a new discussion instead.