#include <stdio.h>
int count_cycle(int n);
int main() {
int N; scanf("%d", &N);
printf("%d",count_cycle(N));
return 0;
}
int count_cycle(int n) {
int N = n; int _N;
int count = 0;
int x, y;
while(_N!=N) {
x = N / 10;
y = N % 10;
_N = 10 * y + ((x+y)%10);
count++;
}
return count;
}
I've been just practicing the basic algorithm about the specific natural numbers that have cycle which is represented above. But this code's result is infinite loop. My intended pseudo-code is like this.
function count_cycle
count = 0
==loop==
N = 10x + y (0 <= x,y <= 9)
N' = 10 * y + (x+y) mod 10
if N' == N then
break;
else then
N = N'
count++
continue;
====
return count
How should I make this code work properly?