Skip to main content

Print all sequences from 1 to n in given length k

Here given code implementation process.

// C Program
// Print all sequences from 1 to n in given length k
#include <stdio.h>

// Display calculated result
void printResult(int result[], int k)
{
	for (int i = 0; i < k; ++i)
	{
		printf(" %d", result[i]);
	}
	// Include new line
	printf("\n");
}
void subSequences(int result[], int num, int count, int k)
{
	if (count == k)
	{
		// Print new result
		printResult(result, k);
		return;
	}
	else
	{
		for (int i = 1; i <= num; ++i)
		{
			// Get result value
			result[count] = i;
			// find next subsequences using recursion
			subSequences(result, num, count + 1, k);
		}
	}
}
void findSequence(int n, int k)
{
	if (n <= 0 || k <= 0)
	{
		return;
	}
	// Auxiliary array
	int result[k];
	subSequences(result, n, 0, k);
}
int main()
{
	int n = 2;
	int k = 4;
	// Test
	findSequence(n, k);
	return 0;
}

Output

 1 1 1 1
 1 1 1 2
 1 1 2 1
 1 1 2 2
 1 2 1 1
 1 2 1 2
 1 2 2 1
 1 2 2 2
 2 1 1 1
 2 1 1 2
 2 1 2 1
 2 1 2 2
 2 2 1 1
 2 2 1 2
 2 2 2 1
 2 2 2 2
// Java Program
// Print all sequences from 1 to n in given length k
class Combination
{
	// Display calculated result
	public void printResult(int[] result, int k)
	{
		for (int i = 0; i < k; ++i)
		{
			System.out.print(" " + result[i]);
		}
		// Include new line
		System.out.print("\n");
	}
	public void subSequences(
      int[] result, 
      int num, 
      int count, 
      int k)
	{
		if (count == k)
		{
			// Print new result
			printResult(result, k);
			return;
		}
		else
		{
			for (int i = 1; i <= num; ++i)
			{
				// Get result value
				result[count] = i;
				// find next subsequences using recursion
				subSequences(result, num, count + 1, k);
			}
		}
	}
	public void findSequence(int n, int k)
	{
		if (n <= 0 || k <= 0)
		{
			return;
		}
		// Auxiliary array
		int[] result = new int[k];
		subSequences(result, n, 0, k);
	}
	public static void main(String[] args)
	{
		Combination task = new Combination();
		int n = 2;
		int k = 4;
		// Test
		task.findSequence(n, k);
	}
}

Output

 1 1 1 1
 1 1 1 2
 1 1 2 1
 1 1 2 2
 1 2 1 1
 1 2 1 2
 1 2 2 1
 1 2 2 2
 2 1 1 1
 2 1 1 2
 2 1 2 1
 2 1 2 2
 2 2 1 1
 2 2 1 2
 2 2 2 1
 2 2 2 2
// Include header file
#include <iostream>
using namespace std;
// C++ Program
// Print all sequences from 1 to n in given length k
class Combination
{
	public:
		// Display calculated result
		void printResult(int result[], int k)
		{
			for (int i = 0; i < k; ++i)
			{
				cout << " " << result[i];
			}
			// Include new line
			cout << "\n";
		}
	void subSequences(int result[], int num, int count, int k)
	{
		if (count == k)
		{
			// Print new result
			this->printResult(result, k);
			return;
		}
		else
		{
			for (int i = 1; i <= num; ++i)
			{
				// Get result value
				result[count] = i;
				// find next subsequences using recursion
				this->subSequences(result, num, count + 1, k);
			}
		}
	}
	void findSequence(int n, int k)
	{
		if (n <= 0 || k <= 0)
		{
			return;
		}
		// Auxiliary array
		int result[k];
		this->subSequences(result, n, 0, k);
	}
};
int main()
{
	Combination *task = new Combination();
	int n = 2;
	int k = 4;
	// Test
	task->findSequence(n, k);
	return 0;
}

Output

 1 1 1 1
 1 1 1 2
 1 1 2 1
 1 1 2 2
 1 2 1 1
 1 2 1 2
 1 2 2 1
 1 2 2 2
 2 1 1 1
 2 1 1 2
 2 1 2 1
 2 1 2 2
 2 2 1 1
 2 2 1 2
 2 2 2 1
 2 2 2 2
// Include namespace system
using System;
// Csharp Program
// Print all sequences from 1 to n in given length k
public class Combination
{
	// Display calculated result
	public void printResult(int[] result, int k)
	{
		for (int i = 0; i < k; ++i)
		{
			Console.Write(" " + result[i]);
		}
		// Include new line
		Console.Write("\n");
	}
	public void subSequences(int[] result, int num, int count, int k)
	{
		if (count == k)
		{
			// Print new result
			this.printResult(result, k);
			return;
		}
		else
		{
			for (int i = 1; i <= num; ++i)
			{
				// Get result value
				result[count] = i;
				// find next subsequences using recursion
				this.subSequences(result, num, count + 1, k);
			}
		}
	}
	public void findSequence(int n, int k)
	{
		if (n <= 0 || k <= 0)
		{
			return;
		}
		// Auxiliary array
		int[] result = new int[k];
		this.subSequences(result, n, 0, k);
	}
	public static void Main(String[] args)
	{
		Combination task = new Combination();
		int n = 2;
		int k = 4;
		// Test
		task.findSequence(n, k);
	}
}

Output

 1 1 1 1
 1 1 1 2
 1 1 2 1
 1 1 2 2
 1 2 1 1
 1 2 1 2
 1 2 2 1
 1 2 2 2
 2 1 1 1
 2 1 1 2
 2 1 2 1
 2 1 2 2
 2 2 1 1
 2 2 1 2
 2 2 2 1
 2 2 2 2
package main
import "fmt"
// Go Program
// Print all sequences from 1 to n in given length k
type Combination struct {}
func getCombination() * Combination {
	var me *Combination = &Combination {}
	return me
}
// Display calculated result
func(this Combination) printResult(result[] int, k int) {
	for i := 0 ; i < k ; i++ {
		fmt.Print(" ", result[i])
	}
	// Include new line
	fmt.Print("\n")
}
func(this Combination) subSequences(result[] int, 
	num int, count int, k int) {
	if count == k {
		// Print new result
		this.printResult(result, k)
		return
	} else {
		for i := 1 ; i <= num ; i++ {
			// Get result value
			result[count] = i
			// find next subsequences using recursion
			this.subSequences(result, num, count + 1, k)
		}
	}
}
func(this Combination) findSequence(n, k int) {
	if n <= 0 || k <= 0 {
		return
	}
	// Auxiliary array
	var result = make([] int, k)
	this.subSequences(result, n, 0, k)
}
func main() {
	var task * Combination = getCombination()
	var n int = 2
	var k int = 4
	// Test
	task.findSequence(n, k)
}

Output

 1 1 1 1
 1 1 1 2
 1 1 2 1
 1 1 2 2
 1 2 1 1
 1 2 1 2
 1 2 2 1
 1 2 2 2
 2 1 1 1
 2 1 1 2
 2 1 2 1
 2 1 2 2
 2 2 1 1
 2 2 1 2
 2 2 2 1
 2 2 2 2
<?php
// Php Program
// Print all sequences from 1 to n in given length k
class Combination
{
	// Display calculated result
	public	function printResult($result, $k)
	{
		for ($i = 0; $i < $k; ++$i)
		{
			echo(" ".$result[$i]);
		}
		// Include new line
		echo("\n");
	}
	public	function subSequences($result, $num, $count, $k)
	{
		if ($count == $k)
		{
			// Print new result
			$this->printResult($result, $k);
			return;
		}
		else
		{
			for ($i = 1; $i <= $num; ++$i)
			{
				// Get result value
				$result[$count] = $i;
				// find next subsequences using recursion
				$this->subSequences($result, $num, $count + 1, $k);
			}
		}
	}
	public	function findSequence($n, $k)
	{
		if ($n <= 0 || $k <= 0)
		{
			return;
		}
		// Auxiliary array
		$result = array_fill(0, $k, 0);
		$this->subSequences($result, $n, 0, $k);
	}
}

function main()
{
	$task = new Combination();
	$n = 2;
	$k = 4;
	// Test
	$task->findSequence($n, $k);
}
main();

Output

 1 1 1 1
 1 1 1 2
 1 1 2 1
 1 1 2 2
 1 2 1 1
 1 2 1 2
 1 2 2 1
 1 2 2 2
 2 1 1 1
 2 1 1 2
 2 1 2 1
 2 1 2 2
 2 2 1 1
 2 2 1 2
 2 2 2 1
 2 2 2 2
// Node JS Program
// Print all sequences from 1 to n in given length k
class Combination
{
	// Display calculated result
	printResult(result, k)
	{
		for (var i = 0; i < k; ++i)
		{
			process.stdout.write(" " + result[i]);
		}
		// Include new line
		process.stdout.write("\n");
	}
	subSequences(result, num, count, k)
	{
		if (count == k)
		{
			// Print new result
			this.printResult(result, k);
			return;
		}
		else
		{
			for (var i = 1; i <= num; ++i)
			{
				// Get result value
				result[count] = i;
				// find next subsequences using recursion
				this.subSequences(result, num, count + 1, k);
			}
		}
	}
	findSequence(n, k)
	{
		if (n <= 0 || k <= 0)
		{
			return;
		}
		// Auxiliary array
		var result = Array(k).fill(0);
		this.subSequences(result, n, 0, k);
	}
}

function main()
{
	var task = new Combination();
	var n = 2;
	var k = 4;
	// Test
	task.findSequence(n, k);
}
main();

Output

 1 1 1 1
 1 1 1 2
 1 1 2 1
 1 1 2 2
 1 2 1 1
 1 2 1 2
 1 2 2 1
 1 2 2 2
 2 1 1 1
 2 1 1 2
 2 1 2 1
 2 1 2 2
 2 2 1 1
 2 2 1 2
 2 2 2 1
 2 2 2 2
#  Python 3 Program
#  Print all sequences from 1 to n in given length k
class Combination :
	#  Display calculated result
	def printResult(self, result, k) :
		i = 0
		while (i < k) :
			print(" ", result[i], end = "")
			i += 1
		
		#  Include new line
		print(end = "\n")
	
	def subSequences(self, result, num, count, k) :
		if (count == k) :
			#  Print new result
			self.printResult(result, k)
			return
		else :
			i = 1
			while (i <= num) :
				#  Get result value
				result[count] = i
				#  find next subsequences using recursion
				self.subSequences(result, num, count + 1, k)
				i += 1
			
		
	
	def findSequence(self, n, k) :
		if (n <= 0 or k <= 0) :
			return
		
		#  Auxiliary list
		result = [0] * (k)
		self.subSequences(result, n, 0, k)
	

def main() :
	task = Combination()
	n = 2
	k = 4
	#  Test
	task.findSequence(n, k)

if __name__ == "__main__": main()

Output

  1  1  1  1
  1  1  1  2
  1  1  2  1
  1  1  2  2
  1  2  1  1
  1  2  1  2
  1  2  2  1
  1  2  2  2
  2  1  1  1
  2  1  1  2
  2  1  2  1
  2  1  2  2
  2  2  1  1
  2  2  1  2
  2  2  2  1
  2  2  2  2
#  Ruby Program
#  Print all sequences from 1 to n in given length k
class Combination 
	#  Display calculated result
	def printResult(result, k) 
		i = 0
		while (i < k) 
			print(" ", result[i])
			i += 1
		end

		#  Include new line
		print("\n")
	end

	def subSequences(result, num, count, k) 
		if (count == k) 
			#  Print new result
			self.printResult(result, k)
			return
		else
 
			i = 1
			while (i <= num) 
				#  Get result value
				result[count] = i
				#  find next subsequences using recursion
				self.subSequences(result, num, count + 1, k)
				i += 1
			end

		end

	end

	def findSequence(n, k) 
		if (n <= 0 || k <= 0) 
			return
		end

		#  Auxiliary array
		result = Array.new(k) {0}
		self.subSequences(result, n, 0, k)
	end

end

def main() 
	task = Combination.new()
	n = 2
	k = 4
	#  Test
	task.findSequence(n, k)
end

main()

Output

 1 1 1 1
 1 1 1 2
 1 1 2 1
 1 1 2 2
 1 2 1 1
 1 2 1 2
 1 2 2 1
 1 2 2 2
 2 1 1 1
 2 1 1 2
 2 1 2 1
 2 1 2 2
 2 2 1 1
 2 2 1 2
 2 2 2 1
 2 2 2 2
// Scala Program
// Print all sequences from 1 to n in given length k
class Combination()
{
	// Display calculated result
	def printResult(result: Array[Int], k: Int): Unit = {
		var i: Int = 0;
		while (i < k)
		{
			print(" " + result(i));
			i += 1;
		}
		// Include new line
		print("\n");
	}
	def subSequences(result: Array[Int], 
      num: Int, count: Int, k: Int): Unit = {
		if (count == k)
		{
			// Print new result
			printResult(result, k);
			return;
		}
		else
		{
			var i: Int = 1;
			while (i <= num)
			{
				// Get result value
				result(count) = i;
				// find next subsequences using recursion
				subSequences(result, num, count + 1, k);
				i += 1;
			}
		}
	}
	def findSequence(n: Int, k: Int): Unit = {
		if (n <= 0 || k <= 0)
		{
			return;
		}
		// Auxiliary array
		var result: Array[Int] = Array.fill[Int](k)(0);
		subSequences(result, n, 0, k);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Combination = new Combination();
		var n: Int = 2;
		var k: Int = 4;
		// Test
		task.findSequence(n, k);
	}
}

Output

 1 1 1 1
 1 1 1 2
 1 1 2 1
 1 1 2 2
 1 2 1 1
 1 2 1 2
 1 2 2 1
 1 2 2 2
 2 1 1 1
 2 1 1 2
 2 1 2 1
 2 1 2 2
 2 2 1 1
 2 2 1 2
 2 2 2 1
 2 2 2 2
// Swift 4 Program
// Print all sequences from 1 to n in given length k
class Combination
{
	// Display calculated result
	func printResult(_ result: [Int], _ k: Int)
	{
		var i: Int = 0;
		while (i < k)
		{
			print(" ", result[i], terminator: "");
			i += 1;
		}
		// Include new line
		print(terminator: "\n");
	}
	func subSequences(
      _ result: inout[Int], 
      _ num: Int, 
        _ count: Int, 
          _ k: Int)
	{
		if (count == k)
		{
			// Print new result
			self.printResult(result, k);
			return;
		}
		else
		{
			var i: Int = 1;
			while (i <= num)
			{
				// Get result value
				result[count] = i;
				// find next subsequences using recursion
				self.subSequences(&result, num, count + 1, k);
				i += 1;
			}
		}
	}
	func findSequence(_ n: Int, _ k: Int)
	{
		if (n <= 0 || k <= 0)
		{
			return;
		}
		// Auxiliary array
		var result: [Int] = Array(repeating: 0, count: k);
		self.subSequences(&result, n, 0, k);
	}
}
func main()
{
	let task: Combination = Combination();
	let n: Int = 2;
	let k: Int = 4;
	// Test
	task.findSequence(n, k);
}
main();

Output

  1  1  1  1
  1  1  1  2
  1  1  2  1
  1  1  2  2
  1  2  1  1
  1  2  1  2
  1  2  2  1
  1  2  2  2
  2  1  1  1
  2  1  1  2
  2  1  2  1
  2  1  2  2
  2  2  1  1
  2  2  1  2
  2  2  2  1
  2  2  2  2
// Kotlin Program
// Print all sequences from 1 to n in given length k
class Combination
{
	// Display calculated result
	fun printResult(result: Array < Int > , k: Int): Unit
	{
		var i: Int = 0;
		while (i < k)
		{
			print(" " + result[i]);
			i += 1;
		}
		// Include new line
		print("\n");
	}
	fun subSequences(
      result: Array < Int > , 
      num: Int, count: Int, k: Int): Unit
	{
		if (count == k)
		{
			// Print new result
			this.printResult(result, k);
			return;
		}
		else
		{
			var i: Int = 1;
			while (i <= num)
			{
				// Get result value
				result[count] = i;
				// find next subsequences using recursion
				this.subSequences(result, num, count + 1, k);
				i += 1;
			}
		}
	}
	fun findSequence(n: Int, k: Int): Unit
	{
		if (n <= 0 || k <= 0)
		{
			return;
		}
		// Auxiliary array
		val result: Array < Int > = Array(k)
		{
			0
		};
		this.subSequences(result, n, 0, k);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Combination = Combination();
	val n: Int = 2;
	val k: Int = 4;
	// Test
	task.findSequence(n, k);
}

Output

 1 1 1 1
 1 1 1 2
 1 1 2 1
 1 1 2 2
 1 2 1 1
 1 2 1 2
 1 2 2 1
 1 2 2 2
 2 1 1 1
 2 1 1 2
 2 1 2 1
 2 1 2 2
 2 2 1 1
 2 2 1 2
 2 2 2 1
 2 2 2 2




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