Create Generate Binary numbers
This code is used to generate binary numbers upto n like if n=5 it's answer is 1,10,11,100,101pull/990/head
parent
3c7339e59c
commit
99172bb782
|
@ -0,0 +1,23 @@
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.Queue;
|
||||||
|
public class generatebinary {
|
||||||
|
public static void genrating(int n,Queue <String> sb)
|
||||||
|
{
|
||||||
|
sb.add("1");
|
||||||
|
|
||||||
|
while(n-->0)
|
||||||
|
{
|
||||||
|
String s1=sb.peek();
|
||||||
|
System.out.println(s1);
|
||||||
|
sb.remove();
|
||||||
|
String s2=s1;
|
||||||
|
sb.add(s1+"0");
|
||||||
|
sb.add(s2+"1");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int n=5;
|
||||||
|
Queue <String> sb=new LinkedList<String>();
|
||||||
|
genrating(n,sb);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue