Generate all binary strings with 1 and 0 of size N

Here given code implementation process.

// Include header file
#include <iostream>
using namespace std;
/*
    C++ Program for
    Generate all binary strings with 1 and 0 of size N
*/
class Combination
{
	public: void display(string output, int n, int one, int zero)
	{
		if (zero == n && one == n)
		{
			// Display calculated result
			cout << output << endl;
			return;
		}
		if (zero > n || one > n)
		{
			return;
		}
		if (zero <= n)
		{
			// Find the next combination by recursively
			this->display(output  +  "0", n, one, zero + 1);
			this->display(output  +  "1", n, one + 1, zero);
		}
	}
	void binaryCombination(int n)
	{
		if (n <= 0)
		{
			return;
		}
		cout << "\nGiven n : " << n << endl;
		// Print permutation
		this->display("", n, 0, 0);
	}
};
int main()
{
	Combination *task = new Combination();
	// Test A
	// n = 2
	task->binaryCombination(2);
	// Test B 
	// n = 3
	task->binaryCombination(3);
	return 0;
}

Output

Given n : 2
0011
0101
0110
1001
1010
1100

Given n : 3
000111
001011
001101
001110
010011
010101
010110
011001
011010
011100
100011
100101
100110
101001
101010
101100
110001
110010
110100
111000
/*
    Java Program for
    Generate all binary strings with 1 and 0 of size N
*/
public class Combination
{
	public void display(String output, int n, int one, int zero)
	{
		if (zero == n && one == n)
		{
			// Display calculated result
			System.out.println(output);
			return;
		}
		if (zero > n || one > n)
		{
			return;
		}
		if (zero <= n)
		{
			// Find the next combination by recursively
			display(output + "0", n, one, zero + 1);
			display(output + "1", n, one + 1, zero);
		}
	}
	public void binaryCombination(int n)
	{
		if (n <= 0)
		{
			return;
		}
		System.out.println("\nGiven n : " + n);
		// Print permutation
		display("", n, 0, 0);
	}
	public static void main(String[] args)
	{
		Combination task = new Combination();
		// Test A
		// n = 2
		task.binaryCombination(2);
		// Test B 
		// n = 3
		task.binaryCombination(3);
	}
}

Output

Given n : 2
0011
0101
0110
1001
1010
1100

Given n : 3
000111
001011
001101
001110
010011
010101
010110
011001
011010
011100
100011
100101
100110
101001
101010
101100
110001
110010
110100
111000
// Include namespace system
using System;
/*
    Csharp Program for
    Generate all binary strings with 1 and 0 of size N
*/
public class Combination
{
	public void display(String output, int n, int one, int zero)
	{
		if (zero == n && one == n)
		{
			// Display calculated result
			Console.WriteLine(output);
			return;
		}
		if (zero > n || one > n)
		{
			return;
		}
		if (zero <= n)
		{
			// Find the next combination by recursively
			this.display(output + "0", n, one, zero + 1);
			this.display(output + "1", n, one + 1, zero);
		}
	}
	public void binaryCombination(int n)
	{
		if (n <= 0)
		{
			return;
		}
		Console.WriteLine("\nGiven n : " + n);
		// Print permutation
		this.display("", n, 0, 0);
	}
	public static void Main(String[] args)
	{
		Combination task = new Combination();
		// Test A
		// n = 2
		task.binaryCombination(2);
		// Test B 
		// n = 3
		task.binaryCombination(3);
	}
}

Output

Given n : 2
0011
0101
0110
1001
1010
1100

Given n : 3
000111
001011
001101
001110
010011
010101
010110
011001
011010
011100
100011
100101
100110
101001
101010
101100
110001
110010
110100
111000
<?php
/*
    Php Program for
    Generate all binary strings with 1 and 0 of size N
*/
class Combination
{
	public	function display($output, $n, $one, $zero)
	{
		if ($zero == $n && $one == $n)
		{
			// Display calculated result
			echo($output."\n");
			return;
		}
		if ($zero > $n || $one > $n)
		{
			return;
		}
		if ($zero <= $n)
		{
			// Find the next combination by recursively
			$this->display($output."0", 
                           $n, $one, $zero + 1);
			$this->display($output."1", 
                           $n, $one + 1, $zero);
		}
	}
	public	function binaryCombination($n)
	{
		if ($n <= 0)
		{
			return;
		}
		echo("\nGiven n : ".$n."\n");
		// Print permutation
		$this->display("", $n, 0, 0);
	}
}

function main()
{
	$task = new Combination();
	// Test A
	// n = 2
	$task->binaryCombination(2);
	// Test B 
	// n = 3
	$task->binaryCombination(3);
}
main();

Output

Given n : 2
0011
0101
0110
1001
1010
1100

Given n : 3
000111
001011
001101
001110
010011
010101
010110
011001
011010
011100
100011
100101
100110
101001
101010
101100
110001
110010
110100
111000
/*
    Node JS Program for
    Generate all binary strings with 1 and 0 of size N
*/
class Combination
{
	display(output, n, one, zero)
	{
		if (zero == n && one == n)
		{
			// Display calculated result
			console.log(output);
			return;
		}
		if (zero > n || one > n)
		{
			return;
		}
		if (zero <= n)
		{
			// Find the next combination by recursively
			this.display(output + "0", 
                         n, one, zero + 1);
			this.display(output + "1", 
                         n, one + 1, zero);
		}
	}
	binaryCombination(n)
	{
		if (n <= 0)
		{
			return;
		}
		console.log("\nGiven n : " + n);
		// Print permutation
		this.display("", n, 0, 0);
	}
}

function main()
{
	var task = new Combination();
	// Test A
	// n = 2
	task.binaryCombination(2);
	// Test B 
	// n = 3
	task.binaryCombination(3);
}
main();

Output

Given n : 2
0011
0101
0110
1001
1010
1100

Given n : 3
000111
001011
001101
001110
010011
010101
010110
011001
011010
011100
100011
100101
100110
101001
101010
101100
110001
110010
110100
111000
#    Python 3 Program for
#    Generate all binary strings with 1 and 0 of size N
class Combination :
	def display(self, output, n, one, zero) :
		if (zero == n and one == n) :
			#  Display calculated result
			print(output)
			return
		
		if (zero > n or one > n) :
			return
		
		if (zero <= n) :
			#  Find the next combination by recursively
			self.display(output + "0", n, one, zero + 1)
			self.display(output + "1", n, one + 1, zero)
		
	
	def binaryCombination(self, n) :
		if (n <= 0) :
			return
		
		print("\nGiven n : ", n)
		#  Print permutation
		self.display("", n, 0, 0)
	

def main() :
	task = Combination()
	#  Test A
	#  n = 2
	task.binaryCombination(2)
	#  Test B 
	#  n = 3
	task.binaryCombination(3)

if __name__ == "__main__": main()

Output

Given n :  2
0011
0101
0110
1001
1010
1100

Given n :  3
000111
001011
001101
001110
010011
010101
010110
011001
011010
011100
100011
100101
100110
101001
101010
101100
110001
110010
110100
111000
#    Ruby Program for
#    Generate all binary strings with 1 and 0 of size N
class Combination 
	def display(output, n, one, zero) 
		if (zero == n && one == n) 
			#  Display calculated result
			print(output, "\n")
			return
		end

		if (zero > n || one > n) 
			return
		end

		if (zero <= n) 
			#  Find the next combination by recursively
			self.display(output + "0", n, one, zero + 1)
			self.display(output + "1", n, one + 1, zero)
		end

	end

	def binaryCombination(n) 
		if (n <= 0) 
			return
		end

		print("\nGiven n : ", n, "\n")
		#  Print permutation
		self.display("", n, 0, 0)
	end

end

def main() 
	task = Combination.new()
	#  Test A
	#  n = 2
	task.binaryCombination(2)
	#  Test B 
	#  n = 3
	task.binaryCombination(3)
end

main()

Output

Given n : 2
0011
0101
0110
1001
1010
1100

Given n : 3
000111
001011
001101
001110
010011
010101
010110
011001
011010
011100
100011
100101
100110
101001
101010
101100
110001
110010
110100
111000
/*
    Scala Program for
    Generate all binary strings with 1 and 0 of size N
*/
class Combination()
{
	def display(output: String, n: Int, 
                one: Int, zero: Int): Unit = {
		if (zero == n && one == n)
		{
			// Display calculated result
			println(output);
			return;
		}
		if (zero > n || one > n)
		{
			return;
		}
		if (zero <= n)
		{
			// Find the next combination by recursively
			display(output + "0", n, one, zero + 1);
			display(output + "1", n, one + 1, zero);
		}
	}
	def binaryCombination(n: Int): Unit = {
		if (n <= 0)
		{
			return;
		}
		println("\nGiven n : " + n);
		// Print permutation
		display("", n, 0, 0);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Combination = new Combination();
		// Test A
		// n = 2
		task.binaryCombination(2);
		// Test B 
		// n = 3
		task.binaryCombination(3);
	}
}

Output

Given n : 2
0011
0101
0110
1001
1010
1100

Given n : 3
000111
001011
001101
001110
010011
010101
010110
011001
011010
011100
100011
100101
100110
101001
101010
101100
110001
110010
110100
111000
/*
    Swift 4 Program for
    Generate all binary strings with 1 and 0 of size N
*/
class Combination
{
	func display(_ output: String, _ n: Int, 
                  _ one: Int, _ zero: Int)
	{
		if (zero == n && one == n)
		{
			// Display calculated result
			print(output);
			return;
		}
		if (zero > n || one > n)
		{
			return;
		}
		if (zero <= n)
		{
			// Find the next combination by recursively
			self.display(output + "0", 
                         n, one, zero + 1);
			self.display(output + "1", 
                         n, one + 1, zero);
		}
	}
	func binaryCombination(_ n: Int)
	{
		if (n <= 0)
		{
			return;
		}
		print("\nGiven n : ", n);
		// Print permutation
		self.display("", n, 0, 0);
	}
}
func main()
{
	let task: Combination = Combination();
	// Test A
	// n = 2
	task.binaryCombination(2);
	// Test B 
	// n = 3
	task.binaryCombination(3);
}
main();

Output

Given n :  2
0011
0101
0110
1001
1010
1100

Given n :  3
000111
001011
001101
001110
010011
010101
010110
011001
011010
011100
100011
100101
100110
101001
101010
101100
110001
110010
110100
111000
/*
    Kotlin Program for
    Generate all binary strings with 1 and 0 of size N
*/
class Combination
{
	fun display(output: String, n: Int, one: Int, zero: Int): Unit
	{
		if (zero == n && one == n)
		{
			// Display calculated result
			println(output);
			return;
		}
		if (zero > n || one > n)
		{
			return;
		}
		if (zero <= n)
		{
			// Find the next combination by recursively
			this.display(output + "0", n, one, zero + 1);
			this.display(output + "1", n, one + 1, zero);
		}
	}
	fun binaryCombination(n: Int): Unit
	{
		if (n <= 0)
		{
			return;
		}
		println("\nGiven n : " + n);
		// Print permutation
		this.display("", n, 0, 0);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Combination = Combination();
	// Test A
	// n = 2
	task.binaryCombination(2);
	// Test B 
	// n = 3
	task.binaryCombination(3);
}

Output

Given n : 2
0011
0101
0110
1001
1010
1100

Given n : 3
000111
001011
001101
001110
010011
010101
010110
011001
011010
011100
100011
100101
100110
101001
101010
101100
110001
110010
110100
111000
package main
import "fmt"
/*
    Go Program for
    Generate all binary strings with 1 and 0 of size N
*/
type Combination struct {}
func getCombination() * Combination {
	var me *Combination = &Combination {}
	return me
}
func(this Combination) display(output string, 
                               n int, 
                               one int, 
                               zero int) {
	if zero == n && one == n {
		// Display calculated result
		fmt.Println(output)
		return
	}
	if zero > n || one > n {
		return
	}
	if zero <= n {
		// Find the next combination by recursively
		this.display(output + "0", n, one, zero + 1)
		this.display(output + "1", n, one + 1, zero)
	}
}
func(this Combination) binaryCombination(n int) {
	if n <= 0 {
		return
	}
	fmt.Println("\nGiven n : ", n)
	// Print permutation
	this.display("", n, 0, 0)
}
func main() {
	var task * Combination = getCombination()
	// Test A
	// n = 2
	task.binaryCombination(2)
	// Test B 
	// n = 3
	task.binaryCombination(3)
}

Output

Given n : 2
0011
0101
0110
1001
1010
1100

Given n : 3
000111
001011
001101
001110
010011
010101
010110
011001
011010
011100
100011
100101
100110
101001
101010
101100
110001
110010
110100
111000


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







© 2021, kalkicode.com, All rights reserved