I recognize this is a common theme, but I cannot find what I am looking for in my own code, somewhat making all other answers moot if I cannot find what's wrong.
I have to implement Collatz recursion for whatever value the user is queried for, and then return the number of steps (iterations) it takes for that number to reach one.
int main(void)
{
int m,n,count;
printf("Enter a value for 'n': ");
scanf("%d",&n);
m = n;
count = 0;
if (m != 1) {
if (m%2 == 0); {
m = m/2;
count++;
}
if (m%2 == 1) {
m = m*3+1;
count++;
}
}
if (m==1)
return count;
printf("The number %d takes %d iterations to reach '1'",n,count);
return 0;
}
Any advice would be wonderful. My belief was that this code (if (m != 1)
) would return a new m
value each time, and each time it did, it would add to count.