'C loop stopping variable from incrementing
I recently learned about Collatz's conjecture. I found it fun, so I just wanted to find out if I could create a small program in C capable of checking the numbers up to a defined number and turned out I wasn't. Here is my program
#include <stdlib.h>
#include <stdio.h>
int main()
{
int n = 5;
int u;
for (int i = 0; i < 1000; i++) {
while(n != 4) {
u = n;
if (n % 2 == 0) {
n = n / 2;
}
else {
n = 3 * n + 1;
}
}
n = u + 1;
printf("%d verifies the conjecture \n", n);
}
}
Problem: The output only shows 9's, and won't increment further. I've done the logic (following what my program would do) by hand, and it works, so I'm completely stumped. Let it be reminded that I'm not good in C, I'm still learning, so it's normal if I'm ignorant to really basic things, as I'm learning it as I go. Thanks all.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
