Print combinations of characters of a numeric string which is less than x

Here given code implementation process.

//  C program for
//  Print combinations of characters of a numeric string which is less than x
#include <stdio.h>

#include <string.h>
 //  Print resultant element
void printSequence(char result[], int n)
{
	for (int i = 0; i < n; ++i)
	{
		printf("  %c", result[i]);
	}
	printf("\n");
}
void findCombination(char num[], char result[], 
  				int check, int index, int n, int k, int x)
{
	if (index >= n || k > x)
	{
		return;
	}
	if (k <= x && index > 0)
	{
		// Display result
		printSequence(result, index);
	}
	// Collect similar value
	result[index] = num[check];
	findCombination(num, result, check, 
                    index + 1, n, (k *10) + (num[check] - '0'), x);
	for (int i = check + 1; i < n; ++i)
	{
		// Collect new resultant element
		result[index] = num[i];
		findCombination(num, result, i, 
                        index + 1, n, (k *10) + (num[i] - '0'), x);
	}
}
void combination(char *num, int x)
{
	int n = strlen(num);
	if (n < 0 || x < 0)
	{
		return;
	}
	// Display given value
	printf("\n Given Number : %s\n", num);
	printf("\n Given x : %d\n", x);
	char result[n];
	findCombination(num, result, 0, 0, n, 0, x);
}
int main(int argc, char const *argv[])
{
	// number : 612345
	//      x : 500
	// Here x indicate combination of number 
	// less than or equal to x.
	combination("612345", 500);
	return 0;
}

Output

 Given Number : 612345

 Given x : 500
  6
  6  6
  6  1
  6  2
  6  3
  6  4
  6  5
  1
  1  1
  1  1  1
  1  1  2
  1  1  3
  1  1  4
  1  1  5
  1  2
  1  2  2
  1  2  3
  1  2  4
  1  2  5
  1  3
  1  3  3
  1  3  4
  1  3  5
  1  4
  1  4  4
  1  4  5
  1  5
  1  5  5
  2
  2  2
  2  2  2
  2  2  3
  2  2  4
  2  2  5
  2  3
  2  3  3
  2  3  4
  2  3  5
  2  4
  2  4  4
  2  4  5
  2  5
  2  5  5
  3
  3  3
  3  3  3
  3  3  4
  3  3  5
  3  4
  3  4  4
  3  4  5
  3  5
  3  5  5
  4
  4  4
  4  4  4
  4  4  5
  4  5
  4  5  5
  5
  5  5
/*
    Java Program
    Print combinations of characters of a numeric string which is less than x
*/
public class Combinations
{
	//  Print resultant element
	public void printSequence(char[] result, int n)
	{
		for (int i = 0; i < n; ++i)
		{
			System.out.print(" " + result[i]);
		}
		System.out.print("\n");
	}
	public void findCombination(String num, 
                                char[] result, 
      int check, int index, int n, int k, int x)
	{
		if (index >= n || k > x)
		{
			return;
		}
		if (k <= x && index > 0)
		{
			// Display result
			printSequence(result, index);
		}
		// Collect similar value
		result[index] = num.charAt(check);
		findCombination(num, result, check, 
                        index + 1, n, (k * 10) + (num.charAt(check) - '0'), x);
		for (int i = check + 1; i < n; ++i)
		{
			// Collect new resultant element
			result[index] = num.charAt(i);
			findCombination(num, result, i, index + 1, 
                            n, (k * 10) + (num.charAt(i) - '0'), x);
		}
	}
	public void combination(String num, int x)
	{
		int n = num.length();
		if (n < 0 || x < 0)
		{
			return;
		}
		// Display given value
		System.out.println("\n Given Number : " + num);
		System.out.println("\n Given x : " + x);
		char[] result = new char[n];
		findCombination(num, result, 0, 0, n, 0, x);
	}
	public static void main(String[] args)
	{
		Combinations task = new Combinations();
		// number : 612345
		//      x : 500
		// Here x indicate combination of number 
		// less than or equal to x.
		task.combination("612345", 500);
	}
}

Output

 Given Number : 612345

 Given x : 500
 6
 6 6
 6 1
 6 2
 6 3
 6 4
 6 5
 1
 1 1
 1 1 1
 1 1 2
 1 1 3
 1 1 4
 1 1 5
 1 2
 1 2 2
 1 2 3
 1 2 4
 1 2 5
 1 3
 1 3 3
 1 3 4
 1 3 5
 1 4
 1 4 4
 1 4 5
 1 5
 1 5 5
 2
 2 2
 2 2 2
 2 2 3
 2 2 4
 2 2 5
 2 3
 2 3 3
 2 3 4
 2 3 5
 2 4
 2 4 4
 2 4 5
 2 5
 2 5 5
 3
 3 3
 3 3 3
 3 3 4
 3 3 5
 3 4
 3 4 4
 3 4 5
 3 5
 3 5 5
 4
 4 4
 4 4 4
 4 4 5
 4 5
 4 5 5
 5
 5 5
// Include header file
#include <iostream>

#include <string>

using namespace std;
/*
    C++ Program
    Print combinations of characters of a numeric string which is less than x
*/
class Combinations
{
	public:
		//  Print resultant element
		void printSequence(char result[], int n)
		{
			for (int i = 0; i < n; ++i)
			{
				cout << " " << result[i];
			}
			cout << "\n";
		}
	void findCombination(string num, 
                         char result[], 
      int check, int index, int n, 
        int k, int x)
	{
		if (index >= n || k > x)
		{
			return;
		}
		if (k <= x && index > 0)
		{
			// Display result
			this->printSequence(result, index);
		}
		// Collect similar value
		result[index] = num[check];
		this->findCombination(num, result, 
                              check, index + 1, n, 
                              (k *10) + (num[check] - '0'), x);
		for (int i = check + 1; i < n; ++i)
		{
			// Collect new resultant element
			result[index] = num[i];
			this->findCombination(num, result, 
                                  i, index + 1, n, 
                                  (k *10) + (num[i] - '0'), x);
		}
	}
	void combination(string num, int x)
	{
		int n = num.length();
		if (n < 0 || x < 0)
		{
			return;
		}
		// Display given value
		cout << "\n Given Number : " << num << endl;
		cout << "\n Given x : " << x << endl;
		char result[n];
		this->findCombination(num, result, 0, 0, n, 0, x);
	}
};
int main()
{
	Combinations *task = new Combinations();
	// number : 612345
	//      x : 500
	// Here x indicate combination of number
	// less than or equal to x.
	task->combination("612345", 500);
	return 0;
}

Output

 Given Number : 612345

 Given x : 500
 6
 6 6
 6 1
 6 2
 6 3
 6 4
 6 5
 1
 1 1
 1 1 1
 1 1 2
 1 1 3
 1 1 4
 1 1 5
 1 2
 1 2 2
 1 2 3
 1 2 4
 1 2 5
 1 3
 1 3 3
 1 3 4
 1 3 5
 1 4
 1 4 4
 1 4 5
 1 5
 1 5 5
 2
 2 2
 2 2 2
 2 2 3
 2 2 4
 2 2 5
 2 3
 2 3 3
 2 3 4
 2 3 5
 2 4
 2 4 4
 2 4 5
 2 5
 2 5 5
 3
 3 3
 3 3 3
 3 3 4
 3 3 5
 3 4
 3 4 4
 3 4 5
 3 5
 3 5 5
 4
 4 4
 4 4 4
 4 4 5
 4 5
 4 5 5
 5
 5 5
// Include namespace system
using System;
/*
    Csharp Program
    Print combinations of characters of a numeric string which is less than x
*/
public class Combinations
{
	//  Print resultant element
	public void printSequence(char[] result, int n)
	{
		for (int i = 0; i < n; ++i)
		{
			Console.Write(" " + result[i]);
		}
		Console.Write("\n");
	}
	public void findCombination(String num, 
                                char[] result, 
      int check, int index, int n, int k, int x)
	{
		if (index >= n || k > x)
		{
			return;
		}
		if (k <= x && index > 0)
		{
			// Display result
			this.printSequence(result, index);
		}
		// Collect similar value
		result[index] = num[check];
		this.findCombination(num, result, 
                             check, index + 1, n, 
                             (k * 10) + (num[check] - '0'), x);
		for (int i = check + 1; i < n; ++i)
		{
			// Collect new resultant element
			result[index] = num[i];
			this.findCombination(num, result, i, 
                                 index + 1, n, (k * 10) + (num[i] - '0'), x);
		}
	}
	public void combination(String num, int x)
	{
		int n = num.Length;
		if (n < 0 || x < 0)
		{
			return;
		}
		// Display given value
		Console.WriteLine("\n Given Number : " + num);
		Console.WriteLine("\n Given x : " + x);
		char[] result = new char[n];
		this.findCombination(num, result, 0, 0, n, 0, x);
	}
	public static void Main(String[] args)
	{
		Combinations task = new Combinations();
		// number : 612345
		//      x : 500
		// Here x indicate combination of number
		// less than or equal to x.
		task.combination("612345", 500);
	}
}

Output

 Given Number : 612345

 Given x : 500
 6
 6 6
 6 1
 6 2
 6 3
 6 4
 6 5
 1
 1 1
 1 1 1
 1 1 2
 1 1 3
 1 1 4
 1 1 5
 1 2
 1 2 2
 1 2 3
 1 2 4
 1 2 5
 1 3
 1 3 3
 1 3 4
 1 3 5
 1 4
 1 4 4
 1 4 5
 1 5
 1 5 5
 2
 2 2
 2 2 2
 2 2 3
 2 2 4
 2 2 5
 2 3
 2 3 3
 2 3 4
 2 3 5
 2 4
 2 4 4
 2 4 5
 2 5
 2 5 5
 3
 3 3
 3 3 3
 3 3 4
 3 3 5
 3 4
 3 4 4
 3 4 5
 3 5
 3 5 5
 4
 4 4
 4 4 4
 4 4 5
 4 5
 4 5 5
 5
 5 5
package main
import "fmt"
/*
    Go Program
    Print combinations of characters of a numeric string which is less than x
*/
type Combinations struct {}
func getCombinations() * Combinations {
	var me *Combinations = &Combinations {}
	return me
}
//  Print resultant element
func(this Combinations) printSequence(result[] byte, n int) {
	for i := 0 ; i < n ; i++ {
		fmt.Print(" ", string(result[i]))
	}
	fmt.Print("\n")
}
func(this Combinations) findCombination(num string, 
                                        result[] byte, 
  check int, index int, n int, k int, x int) {
	if index >= n || k > x {
		return
	}
	if k <= x && index > 0 {
		// Display result
		this.printSequence(result, index)
	}
	// Collect similar value
	result[index] = num[check]
	this.findCombination(num, result, check, 
                         index + 1, n, (k * 10) + (int(num[check]) - '0'), x)
	for i := check + 1 ; i < n ; i++ {
		// Collect new resultant element
		result[index] = num[i]
		this.findCombination(num, result, i, 
                             index + 1, n, (k * 10) + (int(num[i]) - '0'), x)
	}
}
func(this Combinations) combination(num string, x int) {
	var n int = len(num)
	if n < 0 || x < 0 {
		return
	}
	// Display given value
	fmt.Println("\n Given Number : ", num)
	fmt.Println("\n Given x : ", x)
	var result = make([] byte, n)
	this.findCombination(num, result, 0, 0, n, 0, x)
}
func main() {
	var task * Combinations = getCombinations()
	// number : 612345
	//      x : 500
	// Here x indicate combination of number
	// less than or equal to x.
	task.combination("612345", 500)
}

Output

 Given Number : 612345

 Given x : 500
 6
 6 6
 6 1
 6 2
 6 3
 6 4
 6 5
 1
 1 1
 1 1 1
 1 1 2
 1 1 3
 1 1 4
 1 1 5
 1 2
 1 2 2
 1 2 3
 1 2 4
 1 2 5
 1 3
 1 3 3
 1 3 4
 1 3 5
 1 4
 1 4 4
 1 4 5
 1 5
 1 5 5
 2
 2 2
 2 2 2
 2 2 3
 2 2 4
 2 2 5
 2 3
 2 3 3
 2 3 4
 2 3 5
 2 4
 2 4 4
 2 4 5
 2 5
 2 5 5
 3
 3 3
 3 3 3
 3 3 4
 3 3 5
 3 4
 3 4 4
 3 4 5
 3 5
 3 5 5
 4
 4 4
 4 4 4
 4 4 5
 4 5
 4 5 5
 5
 5 5
<?php
/*
    Php Program
    Print combinations of characters of a numeric string which is less than x
*/
class Combinations
{
	//  Print resultant element
	public	function printSequence($result, $n)
	{
		for ($i = 0; $i < $n; ++$i)
		{
			echo(" ".$result[$i]);
		}
		echo("\n");
	}
	public	function findCombination($num, $result, 
                                      $check, $index, 
                                      $n, $k, $x)
	{
		if ($index >= $n || $k > $x)
		{
			return;
		}
		if ($k <= $x && $index > 0)
		{
			// Display result
			$this->printSequence($result, $index);
		}
		// Collect similar value
		$result[$index] = $num[$check];
		$this->findCombination($num, $result, $check, 
                               $index + 1, $n, ($k * 10) + (ord($num[$check]) - ord('0')), $x);
		for ($i = $check + 1; $i < $n; ++$i)
		{
			// Collect new resultant element
			$result[$index] = $num[$i];
			$this->findCombination($num, $result, $i, 
                                   $index + 1, $n, ($k * 10) + (ord($num[$i]) - ord('0')), $x);
		}
	}
	public	function combination($num, $x)
	{
		$n = strlen($num);
		if ($n < 0 || $x < 0)
		{
			return;
		}
		// Display given value
		echo("\n Given Number : ".$num.
			"\n");
		echo("\n Given x : ".$x.
			"\n");
		$result = array_fill(0, $n, ' ');
		$this->findCombination($num, $result, 0, 0, $n, 0, $x);
	}
}

function main()
{
	$task = new Combinations();
	// number : 612345
	//      x : 500
	// Here x indicate combination of number
	// less than or equal to x.
	$task->combination("612345", 500);
}
main();

Output

 Given Number : 612345

 Given x : 500
 6
 6 6
 6 1
 6 2
 6 3
 6 4
 6 5
 1
 1 1
 1 1 1
 1 1 2
 1 1 3
 1 1 4
 1 1 5
 1 2
 1 2 2
 1 2 3
 1 2 4
 1 2 5
 1 3
 1 3 3
 1 3 4
 1 3 5
 1 4
 1 4 4
 1 4 5
 1 5
 1 5 5
 2
 2 2
 2 2 2
 2 2 3
 2 2 4
 2 2 5
 2 3
 2 3 3
 2 3 4
 2 3 5
 2 4
 2 4 4
 2 4 5
 2 5
 2 5 5
 3
 3 3
 3 3 3
 3 3 4
 3 3 5
 3 4
 3 4 4
 3 4 5
 3 5
 3 5 5
 4
 4 4
 4 4 4
 4 4 5
 4 5
 4 5 5
 5
 5 5
/*
    Node JS Program
    Print combinations of characters of a numeric string which is less than x
*/
class Combinations
{
	//  Print resultant element
	printSequence(result, n)
	{
		for (var i = 0; i < n; ++i)
		{
			process.stdout.write(" " + result[i]);
		}
		process.stdout.write("\n");
	}
	findCombination(num, result, check, index, n, k, x)
	{
		if (index >= n || k > x)
		{
			return;
		}
		if (k <= x && index > 0)
		{
			// Display result
			this.printSequence(result, index);
		}
		// Collect similar value
		result[index] = num.charAt(check);
		this.findCombination(num, result, check, 
                             index + 1, n, (k * 10) + 
                             (num.charCodeAt(check) - 
                              '0'.charCodeAt(0)), x);
		for (var i = check + 1; i < n; ++i)
		{
			// Collect new resultant element
			result[index] = num.charAt(i);
			this.findCombination(num, result, i, 
                                 index + 1, n, (k * 10) + 
                                 (num.charCodeAt(i) - 48), x);
		}
	}
	combination(num, x)
	{
		var n = num.length;
		if (n < 0 || x < 0)
		{
			return;
		}
		// Display given value
		console.log("\n Given Number : " + num);
		console.log("\n Given x : " + x);
		var result = Array(n).fill(' ');
		this.findCombination(num, result, 0, 0, n, 0, x);
	}
}

function main()
{
	var task = new Combinations();
	// number : 612345
	//      x : 500
	// Here x indicate combination of number
	// less than or equal to x.
	task.combination("612345", 500);
}
main();

Output

 Given Number : 612345

 Given x : 500
 6
 6 6
 6 1
 6 2
 6 3
 6 4
 6 5
 1
 1 1
 1 1 1
 1 1 2
 1 1 3
 1 1 4
 1 1 5
 1 2
 1 2 2
 1 2 3
 1 2 4
 1 2 5
 1 3
 1 3 3
 1 3 4
 1 3 5
 1 4
 1 4 4
 1 4 5
 1 5
 1 5 5
 2
 2 2
 2 2 2
 2 2 3
 2 2 4
 2 2 5
 2 3
 2 3 3
 2 3 4
 2 3 5
 2 4
 2 4 4
 2 4 5
 2 5
 2 5 5
 3
 3 3
 3 3 3
 3 3 4
 3 3 5
 3 4
 3 4 4
 3 4 5
 3 5
 3 5 5
 4
 4 4
 4 4 4
 4 4 5
 4 5
 4 5 5
 5
 5 5
#    Python 3 Program
#    Print combinations of characters of a numeric string which is less than x
class Combinations :
	#   Print resultant element
	def printSequence(self, result, n) :
		i = 0
		while (i < n) :
			print(" ", result[i], end = "")
			i += 1
		
		print(end = "\n")
	
	def findCombination(self, num, result, check, index, n, k, x) :
		if (index >= n or k > x) :
			return
		
		if (k <= x and index > 0) :
			#  Display result
			self.printSequence(result, index)
		
		#  Collect similar value
		result[index] = num[check]
		self.findCombination(num, result, check, 
                             index + 1, n, 
                             (k * 10) + (ord(num[check]) - ord('0')), x)
		i = check + 1
		while (i < n) :
			#  Collect new resultant element
			result[index] = num[i]
			self.findCombination(num, 
                                 result, i, index + 1, n, 
                                 (k * 10) + (ord(num[i]) - ord('0')), x)
			i += 1
		
	
	def combination(self, num, x) :
		n = len(num)
		if (n < 0 or x < 0) :
			return
		
		#  Display given value
		print("\n Given Number : ", num)
		print("\n Given x : ", x)
		result = [ ' ' ] * (n)
		self.findCombination(num, result, 0, 0, n, 0, x)
	

def main() :
	task = Combinations()
	#  number : 612345
	#       x : 500
	#  Here x indicate combination of number
	#  less than or equal to x.
	task.combination("612345", 500)

if __name__ == "__main__": main()

Output

 Given Number :  612345

 Given x :  500
  6
  6  6
  6  1
  6  2
  6  3
  6  4
  6  5
  1
  1  1
  1  1  1
  1  1  2
  1  1  3
  1  1  4
  1  1  5
  1  2
  1  2  2
  1  2  3
  1  2  4
  1  2  5
  1  3
  1  3  3
  1  3  4
  1  3  5
  1  4
  1  4  4
  1  4  5
  1  5
  1  5  5
  2
  2  2
  2  2  2
  2  2  3
  2  2  4
  2  2  5
  2  3
  2  3  3
  2  3  4
  2  3  5
  2  4
  2  4  4
  2  4  5
  2  5
  2  5  5
  3
  3  3
  3  3  3
  3  3  4
  3  3  5
  3  4
  3  4  4
  3  4  5
  3  5
  3  5  5
  4
  4  4
  4  4  4
  4  4  5
  4  5
  4  5  5
  5
  5  5
#    Ruby Program
#    Print combinations of characters of a numeric string which is less than x
class Combinations 
	#   Print resultant element
	def printSequence(result, n) 
		i = 0
		while (i < n) 
			print(" ", result[i])
			i += 1
		end

		print("\n")
	end

	def findCombination(num, result, check, index, n, k, x) 
		if (index >= n || k > x) 
			return
		end

		if (k <= x && index > 0) 
			#  Display result
			self.printSequence(result, index)
		end

		#  Collect similar value
		result[index] = num[check]
		self.findCombination(num, result, 
                             check, index + 1, n, 
                             (k * 10) + (num[check].ord - '0'.ord), x)
		i = check + 1
		while (i < n) 
			#  Collect new resultant element
			result[index] = num[i]
			self.findCombination(num, result, i, 
                                 index + 1, n, 
                                 (k * 10) + (num[i].ord - '0'.ord), x)
			i += 1
		end

	end

	def combination(num, x) 
		n = num.length
		if (n < 0 || x < 0) 
			return
		end

		#  Display given value
		print("\n Given Number : ", num, "\n")
		print("\n Given x : ", x, "\n")
		result = Array.new(n) { ' '
		}
		self.findCombination(num, result, 0, 0, n, 0, x)
	end

end

def main() 
	task = Combinations.new()
	#  number : 612345
	#       x : 500
	#  Here x indicate combination of number
	#  less than or equal to x.
	task.combination("612345", 500)
end

main()

Output

 Given Number : 612345

 Given x : 500
 6
 6 6
 6 1
 6 2
 6 3
 6 4
 6 5
 1
 1 1
 1 1 1
 1 1 2
 1 1 3
 1 1 4
 1 1 5
 1 2
 1 2 2
 1 2 3
 1 2 4
 1 2 5
 1 3
 1 3 3
 1 3 4
 1 3 5
 1 4
 1 4 4
 1 4 5
 1 5
 1 5 5
 2
 2 2
 2 2 2
 2 2 3
 2 2 4
 2 2 5
 2 3
 2 3 3
 2 3 4
 2 3 5
 2 4
 2 4 4
 2 4 5
 2 5
 2 5 5
 3
 3 3
 3 3 3
 3 3 4
 3 3 5
 3 4
 3 4 4
 3 4 5
 3 5
 3 5 5
 4
 4 4
 4 4 4
 4 4 5
 4 5
 4 5 5
 5
 5 5
import scala.collection.mutable._;
/*
    Scala Program
    Print combinations of characters of a numeric string which is less than x
*/
class Combinations()
{
	//  Print resultant element
	def printSequence(result: Array[Char], n: Int): Unit = {
		var i: Int = 0;
		while (i < n)
		{
			print(" " + result(i));
			i += 1;
		}
		print("\n");
	}
	def findCombination(num: String, 
                        result: Array[Char], 
      check: Int, index: Int, n: Int, 
        k: Int, x: Int): Unit = {
		if (index >= n || k > x)
		{
			return;
		}
		if (k <= x && index > 0)
		{
			// Display result
			printSequence(result, index);
		}
		// Collect similar value
		result(index) = num.charAt(check);
		findCombination(num, result, check, 
                        index + 1, n, 
                        (k * 10) + (num.charAt(check).toInt - '0'.toInt), x);
		var i: Int = check + 1;
		while (i < n)
		{
			// Collect new resultant element
			result(index) = num.charAt(i);
			findCombination(num, result, 
                            i, index + 1, n, 
                            (k * 10) + (num.charAt(i).toInt - '0'.toInt), x);
			i += 1;
		}
	}
	def combination(num: String, x: Int): Unit = {
		var n: Int = num.length();
		if (n < 0 || x < 0)
		{
			return;
		}
		// Display given value
		println("\n Given Number : " + num);
		println("\n Given x : " + x);
		var result: Array[Char] = Array.fill[Char](n)(' ');
		findCombination(num, result, 0, 0, n, 0, x);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Combinations = new Combinations();
		// number : 612345
		//      x : 500
		// Here x indicate combination of number
		// less than or equal to x.
		task.combination("612345", 500);
	}
}

Output

 Given Number : 612345

 Given x : 500
 6
 6 6
 6 1
 6 2
 6 3
 6 4
 6 5
 1
 1 1
 1 1 1
 1 1 2
 1 1 3
 1 1 4
 1 1 5
 1 2
 1 2 2
 1 2 3
 1 2 4
 1 2 5
 1 3
 1 3 3
 1 3 4
 1 3 5
 1 4
 1 4 4
 1 4 5
 1 5
 1 5 5
 2
 2 2
 2 2 2
 2 2 3
 2 2 4
 2 2 5
 2 3
 2 3 3
 2 3 4
 2 3 5
 2 4
 2 4 4
 2 4 5
 2 5
 2 5 5
 3
 3 3
 3 3 3
 3 3 4
 3 3 5
 3 4
 3 4 4
 3 4 5
 3 5
 3 5 5
 4
 4 4
 4 4 4
 4 4 5
 4 5
 4 5 5
 5
 5 5
import Foundation;
/*
    Swift 4 Program
    Print combinations of characters of a numeric string which is less than x
*/
class Combinations
{
	//  Print resultant element
	func printSequence(_ result: [Character], _ n: Int)
	{
		var i: Int = 0;
		while (i < n)
		{
			print(" ", result[i], terminator: "");
			i += 1;
		}
		print(terminator: "\n");
	}
	func findCombination(_ num: [Character], 
                         _ result: inout[Character], 
      _ check: Int, _ index: Int, _ n: Int, _ k: Int, _ x: Int)
	{
		if (index >= n || k > x)
		{
			return;
		}
		if (k <= x && index > 0)
		{
			// Display result
			self.printSequence(result, index);
		}
		// Collect similar value
		result[index] = num[check];
		self.findCombination(num, &result, 
                             check, index + 1, n, (k * 10) + 
                             (Int(UnicodeScalar(String(num[check]))!.value) - 
                              Int(UnicodeScalar(String("0"))!.value)), x);
		var i: Int = check + 1;
		while (i < n)
		{
			// Collect new resultant element
			result[index] = num[i];
			self.findCombination(num, &result, i, 
                                 index + 1, n, (k * 10) +
                                 (Int(UnicodeScalar(String(num[i]))!.value) - 
                                  Int(UnicodeScalar(String("0"))!.value)), x);
			i += 1;
		}
	}
	func combination(_ num: String, _ x: Int)
	{
		let n: Int = num.count;
		if (n < 0 || x < 0)
		{
			return;
		}
		// Display given value
		print("\n Given Number : ", num);
		print("\n Given x : ", x);
		var result: [Character] = Array(repeating: " ", count: n);
		self.findCombination(Array(num), &result, 0, 0, n, 0, x);
	}
}
func main()
{
	let task: Combinations = Combinations();
	// number : 612345
	//      x : 500
	// Here x indicate combination of number
	// less than or equal to x.
	task.combination("612345", 500);
}
main();

Output

 Given Number :  612345

 Given x :  500
  6
  6  6
  6  1
  6  2
  6  3
  6  4
  6  5
  1
  1  1
  1  1  1
  1  1  2
  1  1  3
  1  1  4
  1  1  5
  1  2
  1  2  2
  1  2  3
  1  2  4
  1  2  5
  1  3
  1  3  3
  1  3  4
  1  3  5
  1  4
  1  4  4
  1  4  5
  1  5
  1  5  5
  2
  2  2
  2  2  2
  2  2  3
  2  2  4
  2  2  5
  2  3
  2  3  3
  2  3  4
  2  3  5
  2  4
  2  4  4
  2  4  5
  2  5
  2  5  5
  3
  3  3
  3  3  3
  3  3  4
  3  3  5
  3  4
  3  4  4
  3  4  5
  3  5
  3  5  5
  4
  4  4
  4  4  4
  4  4  5
  4  5
  4  5  5
  5
  5  5
/*
    Kotlin Program
    Print combinations of characters of a numeric string which is less than x
*/
class Combinations
{
	//  Print resultant element
	fun printSequence(result: Array < Char > , n: Int): Unit
	{
		var i: Int = 0;
		while (i < n)
		{
			print(" " + result[i]);
			i += 1;
		}
		print("\n");
	}
	fun findCombination(num: String, 
                        result: Array < Char > , 
                        check: Int, index: Int, 
                        n: Int, k: Int, x: Int): Unit
	{
		if (index >= n || k > x)
		{
			return;
		}
		if (k <= x && index > 0)
		{
			// Display result
			this.printSequence(result, index);
		}
		// Collect similar value
		result[index] = num.get(check);
		this.findCombination(num, result, 
                             check, index + 1, n, (k * 10) + 
                             (num.get(check).toInt() - '0'.toInt()), x);
		var i: Int = check + 1;
		while (i < n)
		{
			// Collect new resultant element
			result[index] = num.get(i);
			this.findCombination(num, result, i, 
                                 index + 1, n, (k * 10) + 
                                 (num.get(i).toInt() - '0'.toInt()), x);
			i += 1;
		}
	}
	fun combination(num: String, x: Int): Unit
	{
		val n: Int = num.length;
		if (n < 0 || x < 0)
		{
			return;
		}
		// Display given value
		println("\n Given Number : " + num);
		println("\n Given x : " + x);
		val result: Array < Char > = Array(n)
		{
			' '
		};
		this.findCombination(num, result, 0, 0, n, 0, x);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Combinations = Combinations();
	// number : 612345
	//      x : 500
	// Here x indicate combination of number
	// less than or equal to x.
	task.combination("612345", 500);
}

Output

 Given Number : 612345

 Given x : 500
 6
 6 6
 6 1
 6 2
 6 3
 6 4
 6 5
 1
 1 1
 1 1 1
 1 1 2
 1 1 3
 1 1 4
 1 1 5
 1 2
 1 2 2
 1 2 3
 1 2 4
 1 2 5
 1 3
 1 3 3
 1 3 4
 1 3 5
 1 4
 1 4 4
 1 4 5
 1 5
 1 5 5
 2
 2 2
 2 2 2
 2 2 3
 2 2 4
 2 2 5
 2 3
 2 3 3
 2 3 4
 2 3 5
 2 4
 2 4 4
 2 4 5
 2 5
 2 5 5
 3
 3 3
 3 3 3
 3 3 4
 3 3 5
 3 4
 3 4 4
 3 4 5
 3 5
 3 5 5
 4
 4 4
 4 4 4
 4 4 5
 4 5
 4 5 5
 5
 5 5


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