Skip to main content

Print all possible combinations of given size

Here given code implementation process.

// C program 
// Print all possible combinations of given size
#include <stdio.h>

//Print unique combinations from (1 to n number) with given size
void print_combination(int result[], int number, int size, int next, int location)
{
	if (location == size)
	{
		printf(" [");
		for (int i = 0; i < size; ++i)
		{
			printf(" %d ", result[i]);
		}
		printf("]\n");
	}
	else
	{
		for (int i = next + 1; i <= number; ++i)
		{
			result[location] = i;
			print_combination(result, number, size, i, location + 1);
		}
	}
}
void combination(int number, int size)
{
	if (size <= 0)
	{
		// When combination pair are invalid
		return;
	}
	else if (number < size)
	{
		// When number of element is less than pair
		return;
	}
	//Used to display result
	int result[size];
	printf("\n Range (1 to %d) combinations size %d \n", size, number);
	print_combination(result, number, size, 0, 0);
}
int main()
{
	int number = 6;
	//Combination pair
	int size = 4;
	//Test
	combination(number, size);
	number = 7;
	//Combination pair
	size = 3;
	//Test
	combination(number, size);
	return 0;
}

Output

 Range (1 to 4) combinations size 6
 [ 1  2  3  4 ]
 [ 1  2  3  5 ]
 [ 1  2  3  6 ]
 [ 1  2  4  5 ]
 [ 1  2  4  6 ]
 [ 1  2  5  6 ]
 [ 1  3  4  5 ]
 [ 1  3  4  6 ]
 [ 1  3  5  6 ]
 [ 1  4  5  6 ]
 [ 2  3  4  5 ]
 [ 2  3  4  6 ]
 [ 2  3  5  6 ]
 [ 2  4  5  6 ]
 [ 3  4  5  6 ]

 Range (1 to 3) combinations size 7
 [ 1  2  3 ]
 [ 1  2  4 ]
 [ 1  2  5 ]
 [ 1  2  6 ]
 [ 1  2  7 ]
 [ 1  3  4 ]
 [ 1  3  5 ]
 [ 1  3  6 ]
 [ 1  3  7 ]
 [ 1  4  5 ]
 [ 1  4  6 ]
 [ 1  4  7 ]
 [ 1  5  6 ]
 [ 1  5  7 ]
 [ 1  6  7 ]
 [ 2  3  4 ]
 [ 2  3  5 ]
 [ 2  3  6 ]
 [ 2  3  7 ]
 [ 2  4  5 ]
 [ 2  4  6 ]
 [ 2  4  7 ]
 [ 2  5  6 ]
 [ 2  5  7 ]
 [ 2  6  7 ]
 [ 3  4  5 ]
 [ 3  4  6 ]
 [ 3  4  7 ]
 [ 3  5  6 ]
 [ 3  5  7 ]
 [ 3  6  7 ]
 [ 4  5  6 ]
 [ 4  5  7 ]
 [ 4  6  7 ]
 [ 5  6  7 ]
/*
  Java program 
  Print all possible combinations of given size
*/

class Combinations
{
	//Print unique combinations from (1 to n number) with given size
	public void print_combination(int[] result, int number, int size, int next, int location)
	{
		if (location == size)
		{
			System.out.print(" [");
			for (int i = 0; i < size; ++i)
			{
				System.out.print(" " + result[i] + " ");
			}
			System.out.print("]\n");
		}
		else
		{
			for (int i = next + 1; i <= number; ++i)
			{
				result[location] = i;
				print_combination(result, number, size, i, location + 1);
			}
		}
	}
	public void combination(int number, int size)
	{
		if (size <= 0)
		{
			// When combination pair are invalid
			return;
		}
		else if (number < size)
		{
			// When number of element is less than pair
			return;
		}
		//Used to display result
		int[] result = new int[size];
		System.out.print("\n Range (1 to " + size + ") combinations size " + number + " \n");
		print_combination(result, number, size, 0, 0);
	}
	public static void main(String[] args)
	{
		Combinations obj = new Combinations();
		// range 1..6
		int number = 6;
		//Combination pair
		int size = 4;
		//Test
		obj.combination(number, size);
		// range 1..7
		number = 7;
		//Combination pair
		size = 3;
		//Test
		obj.combination(number, size);
	}
}

Output

 Range (1 to 4) combinations size 6
 [ 1  2  3  4 ]
 [ 1  2  3  5 ]
 [ 1  2  3  6 ]
 [ 1  2  4  5 ]
 [ 1  2  4  6 ]
 [ 1  2  5  6 ]
 [ 1  3  4  5 ]
 [ 1  3  4  6 ]
 [ 1  3  5  6 ]
 [ 1  4  5  6 ]
 [ 2  3  4  5 ]
 [ 2  3  4  6 ]
 [ 2  3  5  6 ]
 [ 2  4  5  6 ]
 [ 3  4  5  6 ]

 Range (1 to 3) combinations size 7
 [ 1  2  3 ]
 [ 1  2  4 ]
 [ 1  2  5 ]
 [ 1  2  6 ]
 [ 1  2  7 ]
 [ 1  3  4 ]
 [ 1  3  5 ]
 [ 1  3  6 ]
 [ 1  3  7 ]
 [ 1  4  5 ]
 [ 1  4  6 ]
 [ 1  4  7 ]
 [ 1  5  6 ]
 [ 1  5  7 ]
 [ 1  6  7 ]
 [ 2  3  4 ]
 [ 2  3  5 ]
 [ 2  3  6 ]
 [ 2  3  7 ]
 [ 2  4  5 ]
 [ 2  4  6 ]
 [ 2  4  7 ]
 [ 2  5  6 ]
 [ 2  5  7 ]
 [ 2  6  7 ]
 [ 3  4  5 ]
 [ 3  4  6 ]
 [ 3  4  7 ]
 [ 3  5  6 ]
 [ 3  5  7 ]
 [ 3  6  7 ]
 [ 4  5  6 ]
 [ 4  5  7 ]
 [ 4  6  7 ]
 [ 5  6  7 ]
//Include header file
#include <iostream>

using namespace std;
/*
  C++ program 
  Print all possible combinations of given size
*/
class Combinations
{
	public:
		//Print unique combinations from (1 to n number) with given size
		void print_combination(int result[], int number, int size, int next, int location)
		{
			if (location == size)
			{
				cout << " [";
				for (int i = 0; i < size; ++i)
				{
					cout << " " << result[i] << " ";
				}
				cout << "]\n";
			}
			else
			{
				for (int i = next + 1; i <= number; ++i)
				{
					result[location] = i;
					this->print_combination(result, number, size, i, location + 1);
				}
			}
		}
	void combination(int number, int size)
	{
		if (size <= 0)
		{
			// When combination pair are invalid
			return;
		}
		else if (number < size)
		{
			// When number of element is less than pair
			return;
		}
		//Used to display result
		int result[size];
		cout << "\n Range (1 to " << size << ") combinations size " << number << " \n";
		this->print_combination(result, number, size, 0, 0);
	}
};
int main()
{
	Combinations obj = Combinations();
	// range 1..6
	int number = 6;
	//Combination pair
	int size = 4;
	//Test
	obj.combination(number, size);
	// range 1..7
	number = 7;
	//Combination pair
	size = 3;
	//Test
	obj.combination(number, size);
	return 0;
}

Output

 Range (1 to 4) combinations size 6
 [ 1  2  3  4 ]
 [ 1  2  3  5 ]
 [ 1  2  3  6 ]
 [ 1  2  4  5 ]
 [ 1  2  4  6 ]
 [ 1  2  5  6 ]
 [ 1  3  4  5 ]
 [ 1  3  4  6 ]
 [ 1  3  5  6 ]
 [ 1  4  5  6 ]
 [ 2  3  4  5 ]
 [ 2  3  4  6 ]
 [ 2  3  5  6 ]
 [ 2  4  5  6 ]
 [ 3  4  5  6 ]

 Range (1 to 3) combinations size 7
 [ 1  2  3 ]
 [ 1  2  4 ]
 [ 1  2  5 ]
 [ 1  2  6 ]
 [ 1  2  7 ]
 [ 1  3  4 ]
 [ 1  3  5 ]
 [ 1  3  6 ]
 [ 1  3  7 ]
 [ 1  4  5 ]
 [ 1  4  6 ]
 [ 1  4  7 ]
 [ 1  5  6 ]
 [ 1  5  7 ]
 [ 1  6  7 ]
 [ 2  3  4 ]
 [ 2  3  5 ]
 [ 2  3  6 ]
 [ 2  3  7 ]
 [ 2  4  5 ]
 [ 2  4  6 ]
 [ 2  4  7 ]
 [ 2  5  6 ]
 [ 2  5  7 ]
 [ 2  6  7 ]
 [ 3  4  5 ]
 [ 3  4  6 ]
 [ 3  4  7 ]
 [ 3  5  6 ]
 [ 3  5  7 ]
 [ 3  6  7 ]
 [ 4  5  6 ]
 [ 4  5  7 ]
 [ 4  6  7 ]
 [ 5  6  7 ]
//Include namespace system
using System;

/*
  C# program 
  Print all possible combinations of given size
*/

class Combinations
{
	//Print unique combinations from (1 to n number) with given size
	public void print_combination(int[] result, int number, int size, int next, int location)
	{
		if (location == size)
		{
			Console.Write(" [");
			for (int i = 0; i < size; ++i)
			{
				Console.Write(" " + result[i] + " ");
			}
			Console.Write("]\n");
		}
		else
		{
			for (int i = next + 1; i <= number; ++i)
			{
				result[location] = i;
				print_combination(result, number, size, i, location + 1);
			}
		}
	}
	public void combination(int number, int size)
	{
		if (size <= 0)
		{
			// When combination pair are invalid
			return;
		}
		else if (number < size)
		{
			// When number of element is less than pair
			return;
		}
		//Used to display result
		int[] result = new int[size];
		Console.Write("\n Range (1 to " + size + ") combinations size " + number + " \n");
		print_combination(result, number, size, 0, 0);
	}
	public static void Main(String[] args)
	{
		Combinations obj = new Combinations();
		// range 1..6
		int number = 6;
		//Combination pair
		int size = 4;
		//Test
		obj.combination(number, size);
		// range 1..7
		number = 7;
		//Combination pair
		size = 3;
		//Test
		obj.combination(number, size);
	}
}

Output

 Range (1 to 4) combinations size 6
 [ 1  2  3  4 ]
 [ 1  2  3  5 ]
 [ 1  2  3  6 ]
 [ 1  2  4  5 ]
 [ 1  2  4  6 ]
 [ 1  2  5  6 ]
 [ 1  3  4  5 ]
 [ 1  3  4  6 ]
 [ 1  3  5  6 ]
 [ 1  4  5  6 ]
 [ 2  3  4  5 ]
 [ 2  3  4  6 ]
 [ 2  3  5  6 ]
 [ 2  4  5  6 ]
 [ 3  4  5  6 ]

 Range (1 to 3) combinations size 7
 [ 1  2  3 ]
 [ 1  2  4 ]
 [ 1  2  5 ]
 [ 1  2  6 ]
 [ 1  2  7 ]
 [ 1  3  4 ]
 [ 1  3  5 ]
 [ 1  3  6 ]
 [ 1  3  7 ]
 [ 1  4  5 ]
 [ 1  4  6 ]
 [ 1  4  7 ]
 [ 1  5  6 ]
 [ 1  5  7 ]
 [ 1  6  7 ]
 [ 2  3  4 ]
 [ 2  3  5 ]
 [ 2  3  6 ]
 [ 2  3  7 ]
 [ 2  4  5 ]
 [ 2  4  6 ]
 [ 2  4  7 ]
 [ 2  5  6 ]
 [ 2  5  7 ]
 [ 2  6  7 ]
 [ 3  4  5 ]
 [ 3  4  6 ]
 [ 3  4  7 ]
 [ 3  5  6 ]
 [ 3  5  7 ]
 [ 3  6  7 ]
 [ 4  5  6 ]
 [ 4  5  7 ]
 [ 4  6  7 ]
 [ 5  6  7 ]
<?php
/*
  Php program 
  Print all possible combinations of given size
*/

class Combinations
{
	//Print unique combinations from (1 to n number) with given size
	public	function print_combination( & $result, $number, $size, $next, $location)
	{
		if ($location == $size)
		{
			echo " [";
			for ($i = 0; $i < $size; ++$i)
			{
				echo " ". $result[$i] ." ";
			}
			echo "]\n";
		}
		else
		{
			for ($i = $next + 1; $i <= $number; ++$i)
			{
				$result[$location] = $i;
				$this->print_combination($result, $number, $size, $i, $location + 1);
			}
		}
	}
	public	function combination($number, $size)
	{
		if ($size <= 0)
		{
			// When combination pair are invalid
			return;
		}
		else if ($number < $size)
		{
			// When number of element is less than pair
			return;
		}
		//Used to display result
		$result = array_fill(0, $size, 0);
		echo "\n Range (1 to ". $size .") combinations size ". $number ." \n";
		$this->print_combination($result, $number, $size, 0, 0);
	}
}

function main()
{
	$obj = new Combinations();
	// range 1..6
	$number = 6;
	//Combination pair
	$size = 4;
	//Test
	$obj->combination($number, $size);
	// range 1..7
	$number = 7;
	//Combination pair
	$size = 3;
	//Test
	$obj->combination($number, $size);
}
main();

Output

 Range (1 to 4) combinations size 6
 [ 1  2  3  4 ]
 [ 1  2  3  5 ]
 [ 1  2  3  6 ]
 [ 1  2  4  5 ]
 [ 1  2  4  6 ]
 [ 1  2  5  6 ]
 [ 1  3  4  5 ]
 [ 1  3  4  6 ]
 [ 1  3  5  6 ]
 [ 1  4  5  6 ]
 [ 2  3  4  5 ]
 [ 2  3  4  6 ]
 [ 2  3  5  6 ]
 [ 2  4  5  6 ]
 [ 3  4  5  6 ]

 Range (1 to 3) combinations size 7
 [ 1  2  3 ]
 [ 1  2  4 ]
 [ 1  2  5 ]
 [ 1  2  6 ]
 [ 1  2  7 ]
 [ 1  3  4 ]
 [ 1  3  5 ]
 [ 1  3  6 ]
 [ 1  3  7 ]
 [ 1  4  5 ]
 [ 1  4  6 ]
 [ 1  4  7 ]
 [ 1  5  6 ]
 [ 1  5  7 ]
 [ 1  6  7 ]
 [ 2  3  4 ]
 [ 2  3  5 ]
 [ 2  3  6 ]
 [ 2  3  7 ]
 [ 2  4  5 ]
 [ 2  4  6 ]
 [ 2  4  7 ]
 [ 2  5  6 ]
 [ 2  5  7 ]
 [ 2  6  7 ]
 [ 3  4  5 ]
 [ 3  4  6 ]
 [ 3  4  7 ]
 [ 3  5  6 ]
 [ 3  5  7 ]
 [ 3  6  7 ]
 [ 4  5  6 ]
 [ 4  5  7 ]
 [ 4  6  7 ]
 [ 5  6  7 ]
/*
  Node Js program 
  Print all possible combinations of given size
*/
class Combinations
{
	//Print unique combinations from (1 to n number) with given size
	print_combination(result, number, size, next, location)
	{
		if (location == size)
		{
			process.stdout.write(" [");
			for (var i = 0; i < size; ++i)
			{
				process.stdout.write(" " + result[i] + " ");
			}
			process.stdout.write("]\n");
		}
		else
		{
			for (var i = next + 1; i <= number; ++i)
			{
				result[location] = i;
				this.print_combination(result, number, size, i, location + 1);
			}
		}
	}
	combination(number, size)
	{
		if (size <= 0)
		{
			// When combination pair are invalid
			return;
		}
		else if (number < size)
		{
			// When number of element is less than pair
			return;
		}
		//Used to display result
		var result = Array(size).fill(0);
		process.stdout.write("\n Range (1 to " + size + ") combinations size " + number + " \n");
		this.print_combination(result, number, size, 0, 0);
	}
}

function main()
{
	var obj = new Combinations();
	// range 1..6
	var number = 6;
	//Combination pair
	var size = 4;
	//Test
	obj.combination(number, size);
	// range 1..7
	number = 7;
	//Combination pair
	size = 3;
	//Test
	obj.combination(number, size);
}
main();

Output

 Range (1 to 4) combinations size 6
 [ 1  2  3  4 ]
 [ 1  2  3  5 ]
 [ 1  2  3  6 ]
 [ 1  2  4  5 ]
 [ 1  2  4  6 ]
 [ 1  2  5  6 ]
 [ 1  3  4  5 ]
 [ 1  3  4  6 ]
 [ 1  3  5  6 ]
 [ 1  4  5  6 ]
 [ 2  3  4  5 ]
 [ 2  3  4  6 ]
 [ 2  3  5  6 ]
 [ 2  4  5  6 ]
 [ 3  4  5  6 ]

 Range (1 to 3) combinations size 7
 [ 1  2  3 ]
 [ 1  2  4 ]
 [ 1  2  5 ]
 [ 1  2  6 ]
 [ 1  2  7 ]
 [ 1  3  4 ]
 [ 1  3  5 ]
 [ 1  3  6 ]
 [ 1  3  7 ]
 [ 1  4  5 ]
 [ 1  4  6 ]
 [ 1  4  7 ]
 [ 1  5  6 ]
 [ 1  5  7 ]
 [ 1  6  7 ]
 [ 2  3  4 ]
 [ 2  3  5 ]
 [ 2  3  6 ]
 [ 2  3  7 ]
 [ 2  4  5 ]
 [ 2  4  6 ]
 [ 2  4  7 ]
 [ 2  5  6 ]
 [ 2  5  7 ]
 [ 2  6  7 ]
 [ 3  4  5 ]
 [ 3  4  6 ]
 [ 3  4  7 ]
 [ 3  5  6 ]
 [ 3  5  7 ]
 [ 3  6  7 ]
 [ 4  5  6 ]
 [ 4  5  7 ]
 [ 4  6  7 ]
 [ 5  6  7 ]
#   Python 3 program 
#   Print all possible combinations of given size

class Combinations :
	# Print unique combinations from (1 to n number) with given size
	def print_combination(self, result, number, size, next, location) :
		i = 0
		if (location == size) :
			print(" [", end = "")
			while (i < size) :
				print(" ", result[i] ," ", end = "")
				i += 1
			
			print("]\n", end = "")
		else :
			i = next + 1
			while (i <= number) :
				result[location] = i
				self.print_combination(result, number, size, i, location + 1)
				i += 1
			
		
	
	def combination(self, number, size) :
		if (size <= 0) :
			#  When combination pair are invalid
			return
		
		elif(number < size) :
			#  When number of element is less than pair
			return
		
		# Used to display result
		result = [0] * (size)
		print("\n Range (1 to ", size ,") combinations size ", number ," \n", end = "")
		self.print_combination(result, number, size, 0, 0)
	

def main() :
	obj = Combinations()
	#  range 1..6
	number = 6
	# Combination pair
	size = 4
	# Test
	obj.combination(number, size)
	#  range 1..7
	number = 7
	# Combination pair
	size = 3
	# Test
	obj.combination(number, size)

if __name__ == "__main__": main()

Output

 Range (1 to  4 ) combinations size  6
 [  1    2    3    4  ]
 [  1    2    3    5  ]
 [  1    2    3    6  ]
 [  1    2    4    5  ]
 [  1    2    4    6  ]
 [  1    2    5    6  ]
 [  1    3    4    5  ]
 [  1    3    4    6  ]
 [  1    3    5    6  ]
 [  1    4    5    6  ]
 [  2    3    4    5  ]
 [  2    3    4    6  ]
 [  2    3    5    6  ]
 [  2    4    5    6  ]
 [  3    4    5    6  ]

 Range (1 to  3 ) combinations size  7
 [  1    2    3  ]
 [  1    2    4  ]
 [  1    2    5  ]
 [  1    2    6  ]
 [  1    2    7  ]
 [  1    3    4  ]
 [  1    3    5  ]
 [  1    3    6  ]
 [  1    3    7  ]
 [  1    4    5  ]
 [  1    4    6  ]
 [  1    4    7  ]
 [  1    5    6  ]
 [  1    5    7  ]
 [  1    6    7  ]
 [  2    3    4  ]
 [  2    3    5  ]
 [  2    3    6  ]
 [  2    3    7  ]
 [  2    4    5  ]
 [  2    4    6  ]
 [  2    4    7  ]
 [  2    5    6  ]
 [  2    5    7  ]
 [  2    6    7  ]
 [  3    4    5  ]
 [  3    4    6  ]
 [  3    4    7  ]
 [  3    5    6  ]
 [  3    5    7  ]
 [  3    6    7  ]
 [  4    5    6  ]
 [  4    5    7  ]
 [  4    6    7  ]
 [  5    6    7  ]
#   Ruby program 
#   Print all possible combinations of given size

class Combinations 
	# Print unique combinations from (1 to n number) with given size
	def print_combination(result, number, size, next_element, location) 
		i = 0
		if (location == size) 
			print(" [")
			while (i < size) 
				print(" ", result[i] ," ")
				i += 1
			end

			print("]\n")
		else 
			i = next_element + 1
			while (i <= number) 
				result[location] = i
				self.print_combination(result, number, size, i, location + 1)
				i += 1
			end

		end

	end

	def combination(number, size) 
		if (size <= 0) 
			#  When combination pair are invalid
			return
		elsif(number < size) 
			#  When number of element is less than pair
			return
		end

		# Used to display result
		result = Array.new(size) {0}
		print("\n Range (1 to ", size ,") combinations size ", number ," \n")
		self.print_combination(result, number, size, 0, 0)
	end

end

def main() 
	obj = Combinations.new()
	#  range 1..6
	number = 6
	# Combination pair
	size = 4
	# Test
	obj.combination(number, size)
	#  range 1..7
	number = 7
	# Combination pair
	size = 3
	# Test
	obj.combination(number, size)
end

main()

Output

 Range (1 to 4) combinations size 6 
 [ 1  2  3  4 ]
 [ 1  2  3  5 ]
 [ 1  2  3  6 ]
 [ 1  2  4  5 ]
 [ 1  2  4  6 ]
 [ 1  2  5  6 ]
 [ 1  3  4  5 ]
 [ 1  3  4  6 ]
 [ 1  3  5  6 ]
 [ 1  4  5  6 ]
 [ 2  3  4  5 ]
 [ 2  3  4  6 ]
 [ 2  3  5  6 ]
 [ 2  4  5  6 ]
 [ 3  4  5  6 ]

 Range (1 to 3) combinations size 7 
 [ 1  2  3 ]
 [ 1  2  4 ]
 [ 1  2  5 ]
 [ 1  2  6 ]
 [ 1  2  7 ]
 [ 1  3  4 ]
 [ 1  3  5 ]
 [ 1  3  6 ]
 [ 1  3  7 ]
 [ 1  4  5 ]
 [ 1  4  6 ]
 [ 1  4  7 ]
 [ 1  5  6 ]
 [ 1  5  7 ]
 [ 1  6  7 ]
 [ 2  3  4 ]
 [ 2  3  5 ]
 [ 2  3  6 ]
 [ 2  3  7 ]
 [ 2  4  5 ]
 [ 2  4  6 ]
 [ 2  4  7 ]
 [ 2  5  6 ]
 [ 2  5  7 ]
 [ 2  6  7 ]
 [ 3  4  5 ]
 [ 3  4  6 ]
 [ 3  4  7 ]
 [ 3  5  6 ]
 [ 3  5  7 ]
 [ 3  6  7 ]
 [ 4  5  6 ]
 [ 4  5  7 ]
 [ 4  6  7 ]
 [ 5  6  7 ]
/*
  Scala program 
  Print all possible combinations of given size
*/
class Combinations
{
	//Print unique combinations from (1 to n number) with given size
	def print_combination(result: Array[Int], number: Int, size: Int, next: Int, location: Int): Unit = {
		var i: Int = 0;
		if (location == size)
		{
			print(" [");
			while (i < size)
			{
				print(" " + result(i) + " ");
				i += 1;
			}
			print("]\n");
		}
		else
		{
			i = next + 1;
			while (i <= number)
			{
				result(location) = i;
				print_combination(result, number, size, i, location + 1);
				i += 1;
			}
		}
	}
	def combination(number: Int, size: Int): Unit = {
		if (size <= 0)
		{
			// When combination pair are invalid
			return;
		}
		else if (number < size)
		{
			// When number of element is less than pair
			return;
		}
		//Used to display result
		var result: Array[Int] = Array.fill[Int](size)(0);
		print("\n Range (1 to " + size + ") combinations size " + number + " \n");
		print_combination(result, number, size, 0, 0);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: Combinations = new Combinations();
		// range 1..6
		var number: Int = 6;
		//Combination pair
		var size: Int = 4;
		//Test
		obj.combination(number, size);
		// range 1..7
		number = 7;
		//Combination pair
		size = 3;
		//Test
		obj.combination(number, size);
	}
}

Output

 Range (1 to 4) combinations size 6
 [ 1  2  3  4 ]
 [ 1  2  3  5 ]
 [ 1  2  3  6 ]
 [ 1  2  4  5 ]
 [ 1  2  4  6 ]
 [ 1  2  5  6 ]
 [ 1  3  4  5 ]
 [ 1  3  4  6 ]
 [ 1  3  5  6 ]
 [ 1  4  5  6 ]
 [ 2  3  4  5 ]
 [ 2  3  4  6 ]
 [ 2  3  5  6 ]
 [ 2  4  5  6 ]
 [ 3  4  5  6 ]

 Range (1 to 3) combinations size 7
 [ 1  2  3 ]
 [ 1  2  4 ]
 [ 1  2  5 ]
 [ 1  2  6 ]
 [ 1  2  7 ]
 [ 1  3  4 ]
 [ 1  3  5 ]
 [ 1  3  6 ]
 [ 1  3  7 ]
 [ 1  4  5 ]
 [ 1  4  6 ]
 [ 1  4  7 ]
 [ 1  5  6 ]
 [ 1  5  7 ]
 [ 1  6  7 ]
 [ 2  3  4 ]
 [ 2  3  5 ]
 [ 2  3  6 ]
 [ 2  3  7 ]
 [ 2  4  5 ]
 [ 2  4  6 ]
 [ 2  4  7 ]
 [ 2  5  6 ]
 [ 2  5  7 ]
 [ 2  6  7 ]
 [ 3  4  5 ]
 [ 3  4  6 ]
 [ 3  4  7 ]
 [ 3  5  6 ]
 [ 3  5  7 ]
 [ 3  6  7 ]
 [ 4  5  6 ]
 [ 4  5  7 ]
 [ 4  6  7 ]
 [ 5  6  7 ]
/*
  Swift 4 program 
  Print all possible combinations of given size
*/
class Combinations
{
	//Print unique combinations from (1 to n number) with given size
	func print_combination(_ result: inout[Int], _ number: Int, _ size: Int, _ next: Int, _ location: Int)
	{
		var i: Int = 0;
		if (location == size)
		{
			print(" [", terminator: "");
			while (i < size)
			{
				print(" ", result[i]," ", terminator: "");
				i += 1;
			}
			print("]\n", terminator: "");
		}
		else
		{
			i = next + 1;
			while (i <= number)
			{
				result[location] = i;
				self.print_combination(&result, number, size, i, location + 1);
				i += 1;
			}
		}
	}
	func combination(_ number: Int, _ size: Int)
	{
		if (size <= 0)
		{
			// When combination pair are invalid
			return;
		}
		else if (number < size)
		{
			// When number of element is less than pair
			return;
		}
		//Used to display result
		var result: [Int] = Array(repeating: 0, count: size);
		print("\n Range (1 to ", size ,") combinations size ", number ," \n", terminator: "");
		self.print_combination(&result, number, size, 0, 0);
	}
}
func main()
{
	let obj: Combinations = Combinations();
	// range 1..6
	var number: Int = 6;
	//Combination pair
	var size: Int = 4;
	//Test
	obj.combination(number, size);
	// range 1..7
	number = 7;
	//Combination pair
	size = 3;
	//Test
	obj.combination(number, size);
}
main();

Output

 Range (1 to  4 ) combinations size  6
 [  1    2    3    4  ]
 [  1    2    3    5  ]
 [  1    2    3    6  ]
 [  1    2    4    5  ]
 [  1    2    4    6  ]
 [  1    2    5    6  ]
 [  1    3    4    5  ]
 [  1    3    4    6  ]
 [  1    3    5    6  ]
 [  1    4    5    6  ]
 [  2    3    4    5  ]
 [  2    3    4    6  ]
 [  2    3    5    6  ]
 [  2    4    5    6  ]
 [  3    4    5    6  ]

 Range (1 to  3 ) combinations size  7
 [  1    2    3  ]
 [  1    2    4  ]
 [  1    2    5  ]
 [  1    2    6  ]
 [  1    2    7  ]
 [  1    3    4  ]
 [  1    3    5  ]
 [  1    3    6  ]
 [  1    3    7  ]
 [  1    4    5  ]
 [  1    4    6  ]
 [  1    4    7  ]
 [  1    5    6  ]
 [  1    5    7  ]
 [  1    6    7  ]
 [  2    3    4  ]
 [  2    3    5  ]
 [  2    3    6  ]
 [  2    3    7  ]
 [  2    4    5  ]
 [  2    4    6  ]
 [  2    4    7  ]
 [  2    5    6  ]
 [  2    5    7  ]
 [  2    6    7  ]
 [  3    4    5  ]
 [  3    4    6  ]
 [  3    4    7  ]
 [  3    5    6  ]
 [  3    5    7  ]
 [  3    6    7  ]
 [  4    5    6  ]
 [  4    5    7  ]
 [  4    6    7  ]
 [  5    6    7  ]




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