From 99172bb782e775ea9a221367e3ac4d833aee5d7a Mon Sep 17 00:00:00 2001 From: simitoor <109592870+simitoor@users.noreply.github.com> Date: Sat, 8 Oct 2022 21:32:36 +0530 Subject: [PATCH] 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 --- .../Java/queues/Generate Binary numbers | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 algorithms/Java/queues/Generate Binary numbers diff --git a/algorithms/Java/queues/Generate Binary numbers b/algorithms/Java/queues/Generate Binary numbers new file mode 100644 index 00000000..2064ca5b --- /dev/null +++ b/algorithms/Java/queues/Generate Binary numbers @@ -0,0 +1,23 @@ +import java.util.LinkedList; +import java.util.Queue; +public class generatebinary { + public static void genrating(int n,Queue 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 sb=new LinkedList(); + genrating(n,sb); + } +}