How to Print remainder of a number when divided by 2 at each iteration

Viewed 35

`

import java.util.Scanner;
public class Remainder {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter an integer number");
        int num = keyboard.nextInt();
    while (num >0) {
        num=num/2;
        System.out.printf("%d", num);
        System.out.printf("R%d\n",num%2);
       
    }
    }`

The output for 9 should be

4R1  
2R0
1R0

instead it is

4R0
2R0
1R1
0 Answers
Related