Question: Need help in java: Write a program that prompts for a number and prints all of its binary digits: Print the remainder number % 2,
Need help in java:
Write a program that prompts for a number and prints all of its binary digits: Print the remainder number % 2, then replace the number with number / 2. Keep going until the number is 0. For example, if the user provides the input 13, the output should be
1 0 1 1
My code so far:
public static void main(String[] args) {
// TODO Auto-generated method stub
//prompt user for a number
int number;
Scanner in = new Scanner(System.in);
System.out.print("Enter Number: ");
number = in.nextInt();
while(number>0) {
int digit = number % 2;
System.out.println(digit);
number = number / 2;
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
