7.7k views
0 votes
Using any loop construct, write a java code to print the following:

1010101
10101
101
1​

1 Answer

7 votes

Answer:

class Main {

public static void main(String[] args) {

for (int n = 0x55; n > 0; n /= 4) {

System.out.println(Integer.toBinaryString(n));

}

}

}

Step-by-step explanation:

Because the pattern is very regular, you can start with 0x55 (which is 0101 0101) and shift right 2 places everytime. Shift right 2 places is the same as dividing by 4.

User Vadim Fint
by
3.9k points