Need help on Looping Statement (C++)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bitong
    New Member
    • Sep 2006
    • 40

    Need help on Looping Statement (C++)

    Here's the problem.. Write a program that will add up all the 10 consecutive integers using for statement. Example: 1+2+3+4+5+6+7+8 +9+10=55..
    output program will be: sum=55.

    can't figure it out...i'm just a newbie in c++.

    Code:
     #include <iostream.h> 
    main()
    {
    int x, sum;
    for(x=1;x<=10;x++)
    {
    sum=x+x;
    }
    cout<<"sum="<<sum;
    cout<<"\n";
    return 0;
    }
  • Soujiro
    New Member
    • Jan 2007
    • 35

    #2
    Originally posted by bitong
    Here's the problem.. Write a program that will add up all the 10 consecutive integers using for statement. Example: 1+2+3+4+5+6+7+8 +9+10=55..
    output program will be: sum=55.

    can't figure it out...i'm just a newbie in c++.

    #include <iostream.h>
    main()
    {
    int x, sum;
    for(x=1;x<=10;x ++)
    {
    sum=x+x;
    }
    cout<<"sum="<<s um;
    cout<<"\n";
    return 0;
    }
    "instantiat e" sum=0;
    in the loop sum+=x; thats it..

    Comment

    Working...