# Loop problem

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 08-22-2007
Tesnik
Loop problem
Hi, first of all i have to say Im a little embarrassed of making this question because Ive been reading a lot of posts out there, and they are really advanced topics. This is really simple.
I'm sure that the solution has to be also very simple, but I've been thinking about it and i just cant find it.
ok here it goes:

I have to add the numbers between 1 and 10 using a loop. I know how to do the loop, what i dont know is how add the numbers between 1 and 10.

I repeat that I know this has to be really stupid :D but i would appreciate some help.

Thanks.
• 08-22-2007
robwhit
Code:

```#include <iostream> int main() {     int num1, num2, num3;     num2 = 5;     num3 = 2;     num1 = num2 + num3;     std::cout << num3 << std::endl;     return 0; }```
• 08-22-2007
Tesnik
Thanks. but ive tried your code, and this isnt what im looking for. What i want to do is to add ALL the numbers between 1 and 10 using loop.
• 08-22-2007
l2u
Quote:

Originally Posted by robwhit
Code:

```#include <iostream> int main() {     int num1, num2, num3;     num2 = 5;     num3 = 2;     num1 = num2 + num3;     std::cout << num3 << std::endl;     return 0; }```

• 08-22-2007
robwhit
what code do you have so far? you said that you knew how to do the loop part.
• 08-22-2007
Tesnik
I dont see any loop there. Imagine a want to do it with a while loop. How would I do it?
• 08-22-2007
Tesnik
ok this is what i got:

Code:

``` #include <iostream> using namespace std; int main() { int x; x = 0; while ( x <= 10) {     ????? }```
• 08-22-2007
robwhit
Quote:

Originally Posted by Tesnik
How would I do it?

I'd do it by using a counter variable and then using another variable and add successively to the variable the value of the counter each time the loop repeats.

the code should be indented and in between braces like this:
Code:

```#include <iostream> using namespace std; int main() {     int x;     x = 0;     while ( x <= 10)     {         ?????     }     return 0; }```
have you done for loops yet? they would usually be used in this situation, but you can use a while loop too.

here's the general layout in case it isn't clear:
Code:

```    while ( x <= 10)     {         // add y + x to y         // add 1 to x     }```
http://www.cprogramming.com/tutorial/lesson3.html
• 08-22-2007
cpjust
How's this? :p
Code:

```#include <iostream> using namespace std; int AddNums( int begin, int end ) {   if ( begin == end )   {       return begin;   }   else   {       return end + AddNums( begin, end - 1 );   } } int main() {   cout << AddNums( 1, 10 );   return 0; }```
• 08-22-2007
Tesnik
ok umm, from your post ive tried this:

Code:

``` #include <iostream> using namespace std; int main() {   int x;   int y;     x = 1;   y = 1;/*counter*/     while( x <= 10 )   {           cout<< x <<"\n";           y++;           x = x + y;             }     system("pause");   return 0;     }```
but... the output is this:

1
3
6
10

and I dont want that :(
• 08-22-2007
Tesnik
oops i wrote to slow :)

cpjust THATS what i wanted!!

thanks both of you:)

Ive been studying C++ from this page (which is great) for about two weeks, and Ive done simple programs a little bit more complicated than this, thats why im so surprised that i couldnt find a solution for this, i was beggining to think i was stupid. Actually I havent found a solution, somebody else found it, so i think that makes me stupid :D

thanks again
• 08-22-2007
robwhit
the counter is the one that goes 1 2 3 4 5 6 7 8 9 10. When you write this line:

x = x + y;

then it doesn't do that. you want:

y = x + y;

and switch the lines

y++;
y = x + y;

to

y = x + y;
y++;
• 08-22-2007
Tesnik
ok :D thanks very much
• 08-22-2007
cpjust
You can also try this:
Code:

```#include <iostream> using namespace std; int main() {   int n = 0;   int i = 10; loop:   n += i;   if ( --i > 0 )           goto loop;   cout << n << endl;   return 0; }```
• 08-22-2007
Tesnik
wow haha i think thats a little more advanced. I understood the other code better :)

I haven't seen the loop: thing.
and the --i... dont know what it is :D

but i will learn.

thanks again
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last