Hello I'm trying to program a sequence and I think I might have successfully created one ( if not feel free to point out the problem)
while (n>=1)
{ if (n==1)
{ n=1;
break;
}
if (n%2==0)
{ n=n/2;
}
if (n%2==1)
{ n= ((3*n)+(1));
}
}
Assuming n is equal to an integer greater than or equal to 1, n will continue to go through the sequence until it reaches 1 and then it will end.
First I want to know how many times it went to the sequence so I need a code that will tell me how many times it goes through the sequence for a value of n greater than 1
Also I want to print out to the screen the value of n for every time it goes through the sequence. Obviously this will require me to write cout <<" " << endl; and most likely using more variables.
Also my friend said to look for iterations but I can't find it on my book or what I'm referring to here over the web. If someone can lead me to a certain subject I can read about what I want to do. Any references will be nice. TY
int counter = 0;
while (n>=1)
{
if (n==1)
{
n=1;
break;
}
if (n%2==0)
{
n=n/2;
}
if (n%2==1)
{
n= ((3*n)+(1));
}
counter++; //This will keep track of how many times you've gone through the sequence;
cout << n << endl; //This will output the value of n at the end of each sequence.
}
cout << "we went through " << counter << " iterations of the sequence" << endl;