hello im a begginer in java and im trying to "If n is even, divide it in half. If it is odd, multiply it by 3 and add 1. Repeat this operation until reaching 1."
but my output becomes like this
13 40 121 60 181 544 1633 4900 14701 44104 132313 396940 1190821 3572464 10717393 32152180 96456541 289369624 868108873 -1690640676 -776954731 1964103104 1597342017 497058756 1491176269 745588134 -2058202893 -1879641382 -1343956849 263096750 789290251 -1927096542 -1486322329 -163999690 -491999069 -1475997206 -133024321 -399072962 -1197218885 703310642 2109931927 2034828486 1809518163 1133587194 -894205713 1612350158 542083179 271041589 813124768 -1855592991 -1271811676 479532269 1438596808 719298404 359649202 179824601 89912300 269736901 809210704 -1867335183 -1307038252 373852541 1121557624 560778812 280389406 140194703 70097351 210292054 630876163 1892628490 1382918175 -146212770
instead of
13 40 20 10 5 16 8 4 2 1 (yes the output has to be single line with spaces)
what do I change in my code to get the output above??
(here's my code)
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int number = scan.nextInt();
int i = number;
System.out.print(i + " ");
do {
if (i % 11 == 0){
do {
i /= 2;
System.out.print(i + " ");
} while (i % 11 == 0);
}
else if (i % 11 != 0) {
do {
i = i*3+1;
System.out.print(i + " ");
} while (i % 11 != 0);
}
else {
System.out.print("Invalid");
}
} while (i >= 1);
}