0

heres my code:

#include<iostream>
using namespace std;
int main()
{
    int x;
    
      for(x=3; x<=47; x++)
      {
               cout<<x;
               }
         system("pause");
         return 0;
         }

i just want a little help with this problem: this code cout counts from 3 to 47 but i want to cout 3 then adding 2 to cout 5 then adding 3 cout 8. what should i do to make increment increase? exact output would be this 3 5 8 12 17 23 30 38 47. thanks a lot guys.

Edited by WaltP: Added CODE Tags -- Please use them

4
Contributors
8
Replies
9
Views
6 Years
Discussion Span
Last Post by opawix
0

You could use another variable eg y=2, and make x+=y, then increment y in the loop. Although this wouldn't work for the first number, an extra cout before the loop would do the trick though.
Be aware this is probably a very unelegant way of doing it.

Edited by frogboy77: n/a

0

How do you add 2, then 3 to get the values you want? Where would you add 2, then 3 in your program?

0

i don't know,thats exactly what iam asking for. hehe

Edited by opawix: n/a

0

index = 2;
int i = 3;
while(i <= 47){
cout << i;
i = i + index; /*i += index*/
index++;
}

0

Think about it...

How do you add 2, then 3 to get the values you want?

x = x + 2;
x = x + 3;

Where would you add 2, then 3 in your program?

Don't you have a FOR Loop in your program? What does it do?

0

dude thanks a lot! your all the best! especially rannamaa!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.