Through a lot of trial and error I realize that k
can't increment up to the value of length of the outputted binary. So in the case of this code, k
is set to increment up to one less than 4 (k < 4)
so I only could test 8 to 15 since their values in binary are no longer than four in length.
Also, the values are printed out backwards. Any ideas on how to fix these two errors?
The commented lines is when I attempted to use StringBuilder
but that did not work out.
Scanner scan = new Scanner(System.in);
int num = 0;
int test = scan.nextInt();
//StringBuilder sb = new StringBuilder();
for(int i = 0; i < test ; i++) {
int a = scan.nextInt();
for( int k = 0; k < 4; k++ )
{
num = a / 2;
int remainder = a % 2;
//String noodle = Integer.toString(remainder);
a = num;
//sb.append(noodle);
//System.out.print(sb);
System.out.println(remainder);
}
}