0

I am trying to have a my program print out all the potential binary values based on the amount of places given. So for instance if the given amount of places was 2, it would print 00, 01, 10, 11. I have figured out how to get the print out of the binary values, but the leading 0's are being deleted in the single digit values. Is there an efficient way to fix this issue?

Below is my code:

import java.lang.*;

public class TestBit {
   public static void main(String args[]) {
      int index = 3;
      int n = (int) Math.pow(2, index);
      for(int i=0 ; i<n ; i++){
            System.out.println(Integer.toBinaryString(i));
        }
   }
}

Below is my output:

0
1
10
11
100
101
110
111

below is my desired output:

000
001
010
011
100
101
110
111
Drew
  • 1,241
  • 2
  • 10
  • 17
  • read this https://stackoverflow.com/questions/4469717/left-padding-a-string-with-zeros – YCF_L Apr 18 '18 at 17:12
  • Use String.format("%05d", yournumber); Taken from: https://stackoverflow.com/questions/473282/how-can-i-pad-an-integers-with-zeros-on-the-left – JoschJava Apr 18 '18 at 17:13

0 Answers0