How do I write a loop for this?

If I have a random number and if it is even divide it by 7 and if its odd multiply it by 4 and it keeps doing this untill it is equal to 0.

1
2
3
4
5
6
7
int a = 111
int counter = 0

    for (a;a%2==1;a*4)
         for( ??;??;??)

cout<< counter<<endl;
1
2
3
4
5
6
7
8
9
10
11
int main()
{
    int n = 298;
    int counter ;
        for (n;n%2==1;n*4)
            for (n;n%2==0;n/7)
                ++n;
                
                
            
        cout<< counter <<endl;



i do this.. and I get 67. which is wrong.
and I realised if I made counter = 0 it will always output the answer 0
dont know what Im doing wrong.
pseudo code
n = random value <- Initialization
counter = 0 <- Initialization

while n ≠ 0 <- Condition

if n%2 |
n*=4 |
else | Increase
n/=7 |
counter++ |


If you need a for, the form is for (initialization,condition,increase)
You would need the ?: operator for the increase part in this case or you can move it in the block
Last edited on
Im confused.
1
2
3
4
5
6
7
8
9
10
11
for(int n = RANDOM, counter = 0; n != 0; ++counter)
{
if(n%2)
{
  n *= 4;
}
else
{
 n /= 7;
}
}
Last edited on
Topic archived. No new replies allowed.