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.

Recommended Answers

All 8 Replies

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.

thank a lot. ill try this one.

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

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

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

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?

index = 2;
for(int i = 3; i <= 47; index ++){
cout << i;
i += index;
}

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.