Skip to main content

Generate all the binary strings of N bits

Here given code implementation process.

/*
    C program for
    Generate all the binary strings of N bits
*/
#include <stdio.h>

void solution(char record[], int start, int last)
{
	if (start == last)
	{
		printf(" %s \n", record);
		return;
	}
	record[start] = '0';
	solution(record, start + 1, last);
	// change to 1
	record[start] = '1';
	solution(record, start + 1, last);
}
void binaryString(int n)
{
	// N indicate digit in binary
	if (n <= 0)
	{
		return;
	}
	char record[n];
	printf(" Digit : %d \n", n);
	solution(record, 0, n);
}
int main(int argc, char const * argv[])
{
	// Test
	binaryString(4);
	return 0;
}

Output

 Digit : 4
 0000
 0001
 0010
 0011
 0100
 0101
 0110
 0111
 1000
 1001
 1010
 1011
 1100
 1101
 1110
 1111
/*
    Java program for
    Generate all the binary strings of N bits
*/
class BinaryText
{
	public void solution(String record, int start, int last)
	{
		if (start == last)
		{
			System.out.print(" " + record + " \n");
			return;
		}
		// Find result using recursion
		solution(record + '0', start + 1, last);
		solution(record + '1', start + 1, last);
	}
	public void binaryString(int n)
	{
		// N indicate digit in binary
		if (n <= 0)
		{
			return;
		}
		System.out.print(" Digit : " + n + " \n");
		this.solution("", 0, n);
	}
	public static void main(String[] args)
	{
		BinaryText task = new BinaryText();
		task.binaryString(4);
	}
}

Output

 Digit : 4
 0000
 0001
 0010
 0011
 0100
 0101
 0110
 0111
 1000
 1001
 1010
 1011
 1100
 1101
 1110
 1111
// Include header file
#include <iostream>

using namespace std;
/*
    C++ program for
    Generate all the binary strings of N bits
*/
class BinaryText
{
	public: void solution(string record, int start, int last)
	{
		if (start == last)
		{
			cout << " " << record << " \n";
			return;
		}
		// Find result using recursion
		this->solution(record  +  '0', start + 1, last);
		this->solution(record  +  '1', start + 1, last);
	}
	void binaryString(int n)
	{
		// N indicate digit in binary
		if (n <= 0)
		{
			return;
		}
		cout << " Digit : " << n << " \n";
		this->solution("", 0, n);
	}
};
int main()
{
	BinaryText *task = new BinaryText();
	task->binaryString(4);
	return 0;
}

Output

 Digit : 4
 0000
 0001
 0010
 0011
 0100
 0101
 0110
 0111
 1000
 1001
 1010
 1011
 1100
 1101
 1110
 1111
// Include namespace system
using System;
/*
    Csharp program for
    Generate all the binary strings of N bits
*/
public class BinaryText
{
	public void solution(String record, int start, int last)
	{
		if (start == last)
		{
			Console.Write(" " + record + " \n");
			return;
		}
		// Find result using recursion
		this.solution(record + '0', start + 1, last);
		this.solution(record + '1', start + 1, last);
	}
	public void binaryString(int n)
	{
		// N indicate digit in binary
		if (n <= 0)
		{
			return;
		}
		Console.Write(" Digit : " + n + " \n");
		this.solution("", 0, n);
	}
	public static void Main(String[] args)
	{
		BinaryText task = new BinaryText();
		task.binaryString(4);
	}
}

Output

 Digit : 4
 0000
 0001
 0010
 0011
 0100
 0101
 0110
 0111
 1000
 1001
 1010
 1011
 1100
 1101
 1110
 1111
package main
import "fmt"
/*
    Go program for
    Generate all the binary strings of N bits
*/

func solution(record string, start int, last int) {
	if start == last {
		fmt.Print(" ", record, " \n")
		return
	}
	// Find result using recursion
	solution(record +string('0'), start + 1, last)
	solution(record +string('1'), start + 1, last)
}
func binaryString(n int) {
	// N indicate digit in binary
	if n <= 0 {
		return
	}
	fmt.Print(" Digit : ", n, " \n")
	solution("", 0, n)
}
func main() {
	
	binaryString(4)
}

Output

 Digit : 4
 0000
 0001
 0010
 0011
 0100
 0101
 0110
 0111
 1000
 1001
 1010
 1011
 1100
 1101
 1110
 1111
<?php
/*
    Php program for
    Generate all the binary strings of N bits
*/
class BinaryText
{
	public	function solution($record, $start, $last)
	{
		if ($start == $last)
		{
			echo(" ".$record.
				" \n");
			return;
		}
		// Find result using recursion
		$this->solution($record.strval('0'), $start + 1, $last);
		$this->solution($record.strval('1'), $start + 1, $last);
	}
	public	function binaryString($n)
	{
		// N indicate digit in binary
		if ($n <= 0)
		{
			return;
		}
		echo(" Digit : ".$n.
			" \n");
		$this->solution("", 0, $n);
	}
}

function main()
{
	$task = new BinaryText();
	$task->binaryString(4);
}
main();

Output

 Digit : 4
 0000
 0001
 0010
 0011
 0100
 0101
 0110
 0111
 1000
 1001
 1010
 1011
 1100
 1101
 1110
 1111
/*
    Node JS program for
    Generate all the binary strings of N bits
*/
class BinaryText
{
	solution(record, start, last)
	{
		if (start == last)
		{
			process.stdout.write(" " + record + " \n");
			return;
		}
		// Find result using recursion
		this.solution(record + '0', start + 1, last);
		this.solution(record + '1', start + 1, last);
	}
	binaryString(n)
	{
		// N indicate digit in binary
		if (n <= 0)
		{
			return;
		}
		process.stdout.write(" Digit : " + n + " \n");
		this.solution("", 0, n);
	}
}

function main()
{
	var task = new BinaryText();
	task.binaryString(4);
}
main();

Output

 Digit : 4
 0000
 0001
 0010
 0011
 0100
 0101
 0110
 0111
 1000
 1001
 1010
 1011
 1100
 1101
 1110
 1111
#    Python 3 program for
#    Generate all the binary strings of N bits
class BinaryText :
	def solution(self, record, start, last) :
		if (start == last) :
			print(" ", record ," ")
			return
		
		#  Find result using recursion
		self.solution(record + str('0'), start + 1, last)
		self.solution(record + str('1'), start + 1, last)
	
	def binaryString(self, n) :
		#  N indicate digit in binary
		if (n <= 0) :
			return
		
		print(" Digit : ", n ," ")
		self.solution("", 0, n)
	

def main() :
	task = BinaryText()
	task.binaryString(4)

if __name__ == "__main__": main()

Output

 Digit :  4
  0000
  0001
  0010
  0011
  0100
  0101
  0110
  0111
  1000
  1001
  1010
  1011
  1100
  1101
  1110
  1111
#    Ruby program for
#    Generate all the binary strings of N bits
class BinaryText 
	def solution(record, start, last) 
		if (start == last) 
			print(" ", record ," \n")
			return
		end

		#  Find result using recursion
		self.solution(record + '0'.to_s, start + 1, last)
		self.solution(record + '1'.to_s, start + 1, last)
	end

	def binaryString(n) 
		#  N indicate digit in binary
		if (n <= 0) 
			return
		end

		print(" Digit : ", n ," \n")
		self.solution("", 0, n)
	end

end

def main() 
	task = BinaryText.new()
	task.binaryString(4)
end

main()

Output

 Digit : 4 
 0000 
 0001 
 0010 
 0011 
 0100 
 0101 
 0110 
 0111 
 1000 
 1001 
 1010 
 1011 
 1100 
 1101 
 1110 
 1111 
/*
    Scala program for
    Generate all the binary strings of N bits
*/
class BinaryText()
{
	def solution(record: String, start: Int, last: Int): Unit = {
		if (start == last)
		{
			print(" " + record + " \n");
			return;
		}
		// Find result using recursion
		solution(record + '0'.toString(), start + 1, last);
		solution(record + '1'.toString(), start + 1, last);
	}
	def binaryString(n: Int): Unit = {
		// N indicate digit in binary
		if (n <= 0)
		{
			return;
		}
		print(" Digit : " + n + " \n");
		this.solution("", 0, n);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: BinaryText = new BinaryText();
		task.binaryString(4);
	}
}

Output

 Digit : 4
 0000
 0001
 0010
 0011
 0100
 0101
 0110
 0111
 1000
 1001
 1010
 1011
 1100
 1101
 1110
 1111
/*
    Swift 4 program for
    Generate all the binary strings of N bits
*/
class BinaryText
{
	func solution(_ record: String, _ start: Int, _ last: Int)
	{
		if (start == last)
		{
			print(" ", record ," ");
			return;
		}
		// Find result using recursion
		self.solution(record + String("0"), start + 1, last);
		self.solution(record + String("1"), start + 1, last);
	}
	func binaryString(_ n: Int)
	{
		// N indicate digit in binary
		if (n <= 0)
		{
			return;
		}
		print(" Digit : ", n ," ");
		self.solution("", 0, n);
	}
}
func main()
{
	let task: BinaryText = BinaryText();
	task.binaryString(4);
}
main();

Output

 Digit :  4
  0000
  0001
  0010
  0011
  0100
  0101
  0110
  0111
  1000
  1001
  1010
  1011
  1100
  1101
  1110
  1111
/*
    Kotlin program for
    Generate all the binary strings of N bits
*/
class BinaryText
{
	fun solution(record: String, start: Int, last: Int): Unit
	{
		if (start == last)
		{
			print(" " + record + " \n");
			return;
		}
		// Find result using recursion
		this.solution(record + '0'.toString(), start + 1, last);
		this.solution(record + '1'.toString(), start + 1, last);
	}
	fun binaryString(n: Int): Unit
	{
		// N indicate digit in binary
		if (n <= 0)
		{
			return;
		}
		print(" Digit : " + n + " \n");
		this.solution("", 0, n);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: BinaryText = BinaryText();
	task.binaryString(4);
}

Output

 Digit : 4
 0000
 0001
 0010
 0011
 0100
 0101
 0110
 0111
 1000
 1001
 1010
 1011
 1100
 1101
 1110
 1111




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment