{"id":27431,"date":"2018-02-02T21:48:52","date_gmt":"2018-02-02T16:18:52","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27431"},"modified":"2018-10-31T15:51:47","modified_gmt":"2018-10-31T10:21:47","slug":"python-programming-interesting-methode-to-generate-binary-numbers-from-1-to-n","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/python-programming-interesting-methode-to-generate-binary-numbers-from-1-to-n\/","title":{"rendered":"Interesting Method to Generate Binary Numbers from 1 to n"},"content":{"rendered":"<p><span style=\"color: #333300;\"><strong>Method to Generate Binary Numbers<\/strong><\/span><\/p>\n<p>Given a number n, write a function that generates and prints all <a href=\"https:\/\/www.wikitechy.com\/tutorials\/c-programming\/c-program-to-convert-decimal-to-binary\" target=\"_blank\" rel=\"noopener\">binary numbers<\/a> with <a href=\"https:\/\/www.wikitechy.com\/tutorials\/javascript\/validate-decimal-numbers\" target=\"_blank\" rel=\"noopener\">decimal values<\/a> from 1 to n.<span id=\"more-130391\"><\/span><\/p>\n<h3 id=\"examples\"><span style=\"color: #003366;\">Examples:<\/span><\/h3>\n<pre>Input: n = 2\r\nOutput: 1, 10\r\n\r\nInput: n = 5\r\nOutput: 1, 10, 11, 100, 101\r\n<\/pre>\n<p>A simple method is to run a <a href=\"https:\/\/www.wikitechy.com\/tutorials\/python\/python-while-loop\" target=\"_blank\" rel=\"noopener\">loop<\/a> from 1 to n, call decimal to binary inside the loop.<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-31790\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2018\/02\/generate-binary-number.png\" alt=\"Generate binary number\" width=\"205\" height=\"361\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2018\/02\/generate-binary-number.png 205w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2018\/02\/generate-binary-number-170x300.png 170w\" sizes=\"(max-width: 205px) 100vw, 205px\" \/><\/p>\n<p>Following is an interesting method that uses queue data structure to print binary numbers.<\/p>\n<p>1) <strong>Create<\/strong> an empty queue of <a href=\"https:\/\/www.wikitechy.com\/tutorials\/python\/interesting-facts-about-strings-in-python-slicing\" target=\"_blank\" rel=\"noopener\">strings<\/a><br \/>\n2)<strong> Enqueue<\/strong> the first binary number \u201c1\u201d to <a href=\"https:\/\/www.wikitechy.com\/tutorials\/python\/python-data-structure-priority-queue-and-heapq\" target=\"_blank\" rel=\"noopener\">queue<\/a>.<br \/>\n3) Now<strong> run a loop<\/strong> for generating and printing n binary numbers.<br \/>\na) <strong>Dequeue<\/strong> and <strong>Print<\/strong> the front of queue.<br \/>\nb) Append \u201c0\u201d at the end of front item and <strong>enqueue<\/strong> it.<br \/>\nc) Append \u201c<strong>1<\/strong>\u201d at the end of front item and<strong> enqueue<\/strong> it.<\/p>\n[ad type=&#8221;banner&#8221;]\n<h4 id=\"python-programming-method-to-generate-binary-numbers\"><span style=\"color: #008080;\"><strong>Python Programming Method to Generate Binary Numbers<\/strong><\/span><\/h4>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\"># Python program to generate binary numbers from <br\/># 1 to n <br\/> <br\/># This function uses queu data structure to print binary numbers<br\/>def generatePrintBinary(n):<br\/>     <br\/>    # Create an empty queue<br\/>    from Queue import Queue<br\/>    q = Queue()<br\/>     <br\/>    # Enqueu the first binary number<br\/>    q.put(&quot;1&quot;)<br\/> <br\/>    # This loop is like BFS of a tree with 1 as root<br\/>    # 0 as left child and 1 as right child and so on<br\/>    while(n&gt;0):<br\/>        n-= 1<br\/>        # Print the front of queue<br\/>        s1 = q.get() <br\/>        print s1 <br\/>     <br\/>        s2 = s1 # Store s1 before changing it<br\/>     <br\/>        # Append &quot;0&quot; to s1 and enqueue it<br\/>        q.put(s1+&quot;0&quot;)<br\/> <br\/>        # Append &quot;1&quot; to s2 and enqueue it. Note that s2<br\/>        # contains the previous front<br\/>        q.put(s2+&quot;1&quot;)<br\/>     <br\/> <br\/># Driver program to test above function<br\/>n = 10<br\/>generatePrintBinary(n)<br\/> <\/code><\/pre> <\/div>\n<h3 id=\"output\"><span style=\"color: #008000;\">Output:<\/span><\/h3>\n<pre>1\r\n10\r\n11\r\n100\r\n101\r\n110\r\n111\r\n1000\r\n1001\r\n1010<\/pre>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Interesting Method to Generate Binary Numbers from 1 to n Given a number n, write a function that generates and prints all binary numbers<\/p>\n","protected":false},"author":1,"featured_media":31259,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[73012,80124],"tags":[81620,81616,81615,81617,81618,81614,81619],"class_list":["post-27431","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-structures","category-queue","tag-c-program-to-find-binary-equivalent-of-a-number","tag-generate-all-binary-numbers-of-length-n-in-c","tag-generate-all-binary-strings-of-length-n-java","tag-generate-all-binary-strings-of-length-n-python","tag-generate-all-strings-of-n-bits-in-c","tag-generate-binary-numbers-from-1-to-n-python","tag-given-a-string-of-characters-numbers-and-symbols-reverse-only-the-characters-in-the-array"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27431","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=27431"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27431\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media\/31259"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27431"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}