144k views
5 votes
It seems like no matter what I do this code is still giving me the error "

Program end never reached (commonly due to an infinite loop, infinite recursion, or divide/mod by 0).
Tests aborted"
import java.util.Scanner;
public class DivideByTwoLoop {
public static void main (String [] args) {
int userNum = 0;
userNum = 20;
while(userNum >= 1){ System.out.print(userNum + " ");
userNum = userNum/2;
}
if((userNum >1) && !(userNum ==0)){
System.out.print(userNum);
} else {
}
while(userNum !=0) System.out.print(userNum + " ");
System.out.println("");
return; }
}

User Wypul
by
5.0k points

1 Answer

8 votes

Step-by-step explanation:

I ran this on an online compiler with no issues, so I would try to use a different compiler if I were you. For example, sometimes the compiler can run a different program than the one you're currently on.

I would also recommend cleaning up the code. You don't need anything outside of the first part, as the latter 2 parts don't do anything because the number is already 0 by then.

Here's what I ended up with:

int userNum = 20;

while(userNum >= 1){

System.out.print(userNum + " ");

userNum = userNum/2;

}

return;

My output is:

20 10 5 2 1

User Wouter Van Damme
by
5.6k points