Discover the best answers at Westonci.ca, where experts share their insights and knowledge with you. Join our Q&A platform to get precise answers from experts in diverse fields and enhance your understanding. Connect with a community of professionals ready to help you find accurate solutions to your questions quickly and efficiently.

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; }
}


Sagot :

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

Thanks for using our service. We're always here to provide accurate and up-to-date answers to all your queries. We appreciate your time. Please come back anytime for the latest information and answers to your questions. Westonci.ca is here to provide the answers you seek. Return often for more expert solutions.