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,101
pull/990/head
simitoor 2022-10-08 21:32:36 +05:30 committed by GitHub
parent 3c7339e59c
commit 99172bb782
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 23 additions and 0 deletions

View File

@ -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);
}
}