Generate all k digit number whose sum is to x

Here given code implementation process.

// C Program
// Generate all k digit number whose sum is to x
#include <stdio.h>

// Display result
void printSequence(int result[], int k)
{
	for (int i = 0; i < k; ++i)
	{
		printf("%d", result[i]);
	}
	printf("\n ");
}
void findCombination(int result[], 
  int index, int x, int sum, int k)
{
	if (index == k && x == sum)
	{
		// Display calculated result
		printSequence(result, index);
		return;
	}
	if (sum > x || index >= k)
	{
		// Base case when stop process
		return;
	}
	for (int i = 0; i <= 9 && sum + i <= x; ++i)
	{
		if (!(index == 0 && i == 0))
		{
			// Collects resultant value
			result[index] = i;
          	// Find next sequence
			findCombination(result, index + 1, x, sum + i, k);
		}
	}
}
void combination(int x, int k)
{
	if (k <= 0)
	{
		return;
	}
	printf("\n Given x : %d  k : %d \n ", x, k);
	// Collect result
	int result[k];
	// Test
	findCombination(result, 0, x, 0, k);
}
int main()
{
	// Test A
	// Given x : 5  k : 3 
	// 104
	// 113
	// 122
	// 131
	// 140
	// 203
	// 212
	// 221
	// 230
	// 302
	// 311
	// 320
	// 401
	// 410
	// 500
	// -----------
	//   Note [050 , 005 , 041,.....] 
	//   Etc are not valid decimal number
	combination(5, 3);
	// Test B
	// x = 6 sum
	// k = 4 length
	//-----------------
	combination(6, 4);
	return 0;
}

Output

 Given x : 5  k : 3
 104
 113
 122
 131
 140
 203
 212
 221
 230
 302
 311
 320
 401
 410
 500

 Given x : 6  k : 4
 1005
 1014
 1023
 1032
 1041
 1050
 1104
 1113
 1122
 1131
 1140
 1203
 1212
 1221
 1230
 1302
 1311
 1320
 1401
 1410
 1500
 2004
 2013
 2022
 2031
 2040
 2103
 2112
 2121
 2130
 2202
 2211
 2220
 2301
 2310
 2400
 3003
 3012
 3021
 3030
 3102
 3111
 3120
 3201
 3210
 3300
 4002
 4011
 4020
 4101
 4110
 4200
 5001
 5010
 5100
 6000
// Java Program 
// Generate all k digit number whose sum is to x
public class DigitSum
{
	// Display result
	public void printSequence(int[] result, int k)
	{
		for (int i = 0; i < k; ++i)
		{
			System.out.print(result[i]);
		}
		System.out.print("\n ");
	}
	public void findCombination(int[] result, 
                                int index, 
                                int x, 
                                int sum, 
                                int k)
	{
		if (index == k && x == sum)
		{
			// Display calculated result
			printSequence(result, index);
			return;
		}
		if (sum > x || index >= k)
		{
			// Base case when stop process
			return;
		}
		for (int i = 0; i <= 9 && sum + i <= x; ++i)
		{
			if (!(index == 0 && i == 0))
			{
				// Collects resultant value
				result[index] = i;
				// Find next sequence
				findCombination(result, index + 1, x, sum + i, k);
			}
		}
	}
	public void combination(int x, int k)
	{
		if (k <= 0)
		{
			return;
		}
		System.out.print("\n Given x : " + x + " k : " + k + " \n ");
		// Collect result
		int[] result = new int[k];
		// Test
		findCombination(result, 0, x, 0, k);
	}
	public static void main(String args[])
	{
		DigitSum task = new DigitSum();
		// Test A
		// Given x : 5  k : 3 
		// 104
		// 113
		// 122
		// 131
		// 140
		// 203
		// 212
		// 221
		// 230
		// 302
		// 311
		// 320
		// 401
		// 410
		// 500
		// -----------
		// Note [050 , 005 , 041,.....] 
		// Etc are not valid decimal number
		task.combination(5, 3);
		// Test B
		// x = 6 sum
		// k = 4 length
		//-----------------
		task.combination(6, 4);
	}
}

Output

 Given x : 5 k : 3
 104
 113
 122
 131
 140
 203
 212
 221
 230
 302
 311
 320
 401
 410
 500

 Given x : 6 k : 4
 1005
 1014
 1023
 1032
 1041
 1050
 1104
 1113
 1122
 1131
 1140
 1203
 1212
 1221
 1230
 1302
 1311
 1320
 1401
 1410
 1500
 2004
 2013
 2022
 2031
 2040
 2103
 2112
 2121
 2130
 2202
 2211
 2220
 2301
 2310
 2400
 3003
 3012
 3021
 3030
 3102
 3111
 3120
 3201
 3210
 3300
 4002
 4011
 4020
 4101
 4110
 4200
 5001
 5010
 5100
 6000
// Include header file
#include <iostream>
using namespace std;
// C++ Program 
// Generate all k digit number whose sum is to x
class DigitSum
{
	public:
		// Display result
		void printSequence(int result[], int k)
		{
			for (int i = 0; i < k; ++i)
			{
				cout << result[i];
			}
			cout << "\n ";
		}
	void findCombination(int result[], 
      		int index, int x, int sum, int k)
	{
		if (index == k && x == sum)
		{
			// Display calculated result
			this->printSequence(result, index);
			return;
		}
		if (sum > x || index >= k)
		{
			// Base case when stop process
			return;
		}
		for (int i = 0; i <= 9 && sum + i <= x; ++i)
		{
			if (!(index == 0 && i == 0))
			{
				// Collects resultant value
				result[index] = i;
				// Find next sequence
				this->findCombination(result, 
                                      index + 1, 
                                      x, 
                                      sum + i, 
                                      k);
			}
		}
	}
	void combination(int x, int k)
	{
		if (k <= 0)
		{
			return;
		}
		cout << "\n Given x : " << x << " k : " << k << " \n ";
		// Collect result
		int result[k];
		// Test
		this->findCombination(result, 0, x, 0, k);
	}
};
int main()
{
	DigitSum *task = new DigitSum();
	// Test A
	// Given x : 5  k : 3 
	// 104
	// 113
	// 122
	// 131
	// 140
	// 203
	// 212
	// 221
	// 230
	// 302
	// 311
	// 320
	// 401
	// 410
	// 500
	// -----------
	// Note [050 , 005 , 041,.....] 
	// Etc are not valid decimal number
	task->combination(5, 3);
	// Test B
	// x = 6 sum
	// k = 4 length
	//-----------------
	task->combination(6, 4);
	return 0;
}

Output

 Given x : 5 k : 3
 104
 113
 122
 131
 140
 203
 212
 221
 230
 302
 311
 320
 401
 410
 500

 Given x : 6 k : 4
 1005
 1014
 1023
 1032
 1041
 1050
 1104
 1113
 1122
 1131
 1140
 1203
 1212
 1221
 1230
 1302
 1311
 1320
 1401
 1410
 1500
 2004
 2013
 2022
 2031
 2040
 2103
 2112
 2121
 2130
 2202
 2211
 2220
 2301
 2310
 2400
 3003
 3012
 3021
 3030
 3102
 3111
 3120
 3201
 3210
 3300
 4002
 4011
 4020
 4101
 4110
 4200
 5001
 5010
 5100
 6000
// Include namespace system
using System;
// Csharp Program 
// Generate all k digit number whose sum is to x
public class DigitSum
{
	// Display result
	public void printSequence(int[] result, int k)
	{
		for (int i = 0; i < k; ++i)
		{
			Console.Write(result[i]);
		}
		Console.Write("\n ");
	}
	public void findCombination(int[] result, 
                                int index, 
                                int x, 
                                int sum, 
                                int k)
	{
		if (index == k && x == sum)
		{
			// Display calculated result
			this.printSequence(result, index);
			return;
		}
		if (sum > x || index >= k)
		{
			// Base case when stop process
			return;
		}
		for (int i = 0; i <= 9 && sum + i <= x; ++i)
		{
			if (!(index == 0 && i == 0))
			{
				// Collects resultant value
				result[index] = i;
				// Find next sequence
				this.findCombination(result, 
                                     index + 1, 
                                     x, 
                                     sum + i,
                                     k);
			}
		}
	}
	public void combination(int x, int k)
	{
		if (k <= 0)
		{
			return;
		}
		Console.Write("\n Given x : " + x + " k : " + k + " \n ");
		// Collect result
		int[] result = new int[k];
		// Test
		this.findCombination(result, 0, x, 0, k);
	}
	public static void Main(String[] args)
	{
		DigitSum task = new DigitSum();
		// Test A
		// Given x : 5  k : 3 
		// 104
		// 113
		// 122
		// 131
		// 140
		// 203
		// 212
		// 221
		// 230
		// 302
		// 311
		// 320
		// 401
		// 410
		// 500
		// -----------
		// Note [050 , 005 , 041,.....] 
		// Etc are not valid decimal number
		task.combination(5, 3);
		// Test B
		// x = 6 sum
		// k = 4 length
		//-----------------
		task.combination(6, 4);
	}
}

Output

 Given x : 5 k : 3
 104
 113
 122
 131
 140
 203
 212
 221
 230
 302
 311
 320
 401
 410
 500

 Given x : 6 k : 4
 1005
 1014
 1023
 1032
 1041
 1050
 1104
 1113
 1122
 1131
 1140
 1203
 1212
 1221
 1230
 1302
 1311
 1320
 1401
 1410
 1500
 2004
 2013
 2022
 2031
 2040
 2103
 2112
 2121
 2130
 2202
 2211
 2220
 2301
 2310
 2400
 3003
 3012
 3021
 3030
 3102
 3111
 3120
 3201
 3210
 3300
 4002
 4011
 4020
 4101
 4110
 4200
 5001
 5010
 5100
 6000
package main
import "fmt"
// Go Program 
// Generate all k digit number whose sum is to x
type DigitSum struct {}
func getDigitSum() * DigitSum {
	var me *DigitSum = &DigitSum {}
	return me
}
// Display result
func(this DigitSum) printSequence(result[] int, k int) {
	for i := 0 ; i < k ; i++ {
		fmt.Print(result[i])
	}
	fmt.Print("\n ")
}
func(this DigitSum) findCombination(result[] int, 
	index int, 
	x int, 
	sum int, 
	k int) {
	if index == k && x == sum {
		// Display calculated result
		this.printSequence(result, index)
		return
	}
	if sum > x || index >= k {
		// Base case when stop process
		return
	}
	for i := 0 ; i <= 9 && sum + i <= x ; i++ {
		if !(index == 0 && i == 0) {
			// Collects resultant value
			result[index] = i
			// Find next sequence
			this.findCombination(result, 
				index + 1, x, sum + i, k)
		}
	}
}
func(this DigitSum) combination(x, k int) {
	if k <= 0 {
		return
	}
	fmt.Print("\n Given x : ", x, " k : ", k, " \n ")
	// Collect result
	var result = make([] int, k)
	// Test
	this.findCombination(result, 0, x, 0, k)
}
func main() {
	var task * DigitSum = getDigitSum()
	// Test A
	// Given x : 5  k : 3 
	// 104
	// 113
	// 122
	// 131
	// 140
	// 203
	// 212
	// 221
	// 230
	// 302
	// 311
	// 320
	// 401
	// 410
	// 500
	// -----------
	// Note [050 , 005 , 041,.....] 
	// Etc are not valid decimal number
	task.combination(5, 3)
	// Test B
	// x = 6 sum
	// k = 4 length
	//-----------------
	task.combination(6, 4)
}

Output

 Given x : 5 k : 3
 104
 113
 122
 131
 140
 203
 212
 221
 230
 302
 311
 320
 401
 410
 500

 Given x : 6 k : 4
 1005
 1014
 1023
 1032
 1041
 1050
 1104
 1113
 1122
 1131
 1140
 1203
 1212
 1221
 1230
 1302
 1311
 1320
 1401
 1410
 1500
 2004
 2013
 2022
 2031
 2040
 2103
 2112
 2121
 2130
 2202
 2211
 2220
 2301
 2310
 2400
 3003
 3012
 3021
 3030
 3102
 3111
 3120
 3201
 3210
 3300
 4002
 4011
 4020
 4101
 4110
 4200
 5001
 5010
 5100
 6000
<?php
// Php Program 
// Generate all k digit number whose sum is to x
class DigitSum
{
	// Display result
	public	function printSequence($result, $k)
	{
		for ($i = 0; $i < $k; ++$i)
		{
			echo($result[$i]);
		}
		echo("\n ");
	}
	public	function findCombination($result, 
                                      $index, 
                                      $x, 
                                      $sum, 
                                      $k)
	{
		if ($index == $k && $x == $sum)
		{
			// Display calculated result
			$this->printSequence($result, $index);
			return;
		}
		if ($sum > $x || $index >= $k)
		{
			// Base case when stop process
			return;
		}
		for ($i = 0; $i <= 9 && $sum + $i <= $x; ++$i)
		{
			if (!($index == 0 && $i == 0))
			{
				// Collects resultant value
				$result[$index] = $i;
				// Find next sequence
				$this->findCombination($result, 
                                       $index + 1, 
                                       $x, 
                                       $sum + $i,
                                       $k);
			}
		}
	}
	public	function combination($x, $k)
	{
		if ($k <= 0)
		{
			return;
		}
		echo("\n Given x : ".$x.
			" k : ".$k.
			" \n ");
		// Collect result
		$result = array_fill(0, $k, 0);
		// Test
		$this->findCombination($result, 0, $x, 0, $k);
	}
}

function main()
{
	$task = new DigitSum();
	// Test A
	// Given x : 5  k : 3 
	// 104
	// 113
	// 122
	// 131
	// 140
	// 203
	// 212
	// 221
	// 230
	// 302
	// 311
	// 320
	// 401
	// 410
	// 500
	// -----------
	// Note [050 , 005 , 041,.....] 
	// Etc are not valid decimal number
	$task->combination(5, 3);
	// Test B
	// x = 6 sum
	// k = 4 length
	//-----------------
	$task->combination(6, 4);
}
main();

Output

 Given x : 5 k : 3
 104
 113
 122
 131
 140
 203
 212
 221
 230
 302
 311
 320
 401
 410
 500

 Given x : 6 k : 4
 1005
 1014
 1023
 1032
 1041
 1050
 1104
 1113
 1122
 1131
 1140
 1203
 1212
 1221
 1230
 1302
 1311
 1320
 1401
 1410
 1500
 2004
 2013
 2022
 2031
 2040
 2103
 2112
 2121
 2130
 2202
 2211
 2220
 2301
 2310
 2400
 3003
 3012
 3021
 3030
 3102
 3111
 3120
 3201
 3210
 3300
 4002
 4011
 4020
 4101
 4110
 4200
 5001
 5010
 5100
 6000
// Node JS Program 
// Generate all k digit number whose sum is to x
class DigitSum
{
	// Display result
	printSequence(result, k)
	{
		for (var i = 0; i < k; ++i)
		{
			process.stdout.write("" + result[i]);
		}
		process.stdout.write("\n ");
	}
	findCombination(result, index, x, sum, k)
	{
		if (index == k && x == sum)
		{
			// Display calculated result
			this.printSequence(result, index);
			return;
		}
		if (sum > x || index >= k)
		{
			// Base case when stop process
			return;
		}
		for (var i = 0; i <= 9 && sum + i <= x; ++i)
		{
			if (!(index == 0 && i == 0))
			{
				// Collects resultant value
				result[index] = i;
				// Find next sequence
				this.findCombination(result, 
                                     index + 1, 
                                     x, 
                                     sum + i, 
                                     k);
			}
		}
	}
	combination(x, k)
	{
		if (k <= 0)
		{
			return;
		}
		process.stdout.write("\n Given x : " + x + " k : " + k + " \n ");
		// Collect result
		var result = Array(k).fill(0);
		// Test
		this.findCombination(result, 0, x, 0, k);
	}
}

function main()
{
	var task = new DigitSum();
	// Test A
	// Given x : 5  k : 3 
	// 104
	// 113
	// 122
	// 131
	// 140
	// 203
	// 212
	// 221
	// 230
	// 302
	// 311
	// 320
	// 401
	// 410
	// 500
	// -----------
	// Note [050 , 005 , 041,.....] 
	// Etc are not valid decimal number
	task.combination(5, 3);
	// Test B
	// x = 6 sum
	// k = 4 length
	//-----------------
	task.combination(6, 4);
}
main();

Output

 Given x : 5 k : 3
 104
 113
 122
 131
 140
 203
 212
 221
 230
 302
 311
 320
 401
 410
 500

 Given x : 6 k : 4
 1005
 1014
 1023
 1032
 1041
 1050
 1104
 1113
 1122
 1131
 1140
 1203
 1212
 1221
 1230
 1302
 1311
 1320
 1401
 1410
 1500
 2004
 2013
 2022
 2031
 2040
 2103
 2112
 2121
 2130
 2202
 2211
 2220
 2301
 2310
 2400
 3003
 3012
 3021
 3030
 3102
 3111
 3120
 3201
 3210
 3300
 4002
 4011
 4020
 4101
 4110
 4200
 5001
 5010
 5100
 6000
#  Python 3 Program 
#  Generate all k digit number whose sum is to x
class DigitSum :
	#  Display result
	def printSequence(self, result, k) :
		i = 0
		while (i < k) :
			print(result[i], end = "")
			i += 1
		
		print("\n ", end = "")
	
	def findCombination(self, result, index, x, sum, k) :
		if (index == k and x == sum) :
			#  Display calculated result
			self.printSequence(result, index)
			return
		
		if (sum > x or index >= k) :
			#  Base case when stop process
			return
		
		i = 0
		while (i <= 9 and sum + i <= x) :
			if (not(index == 0 and i == 0)) :
				#  Collects resultant value
				result[index] = i
				#  Find next sequence
				self.findCombination(result, 
                                     index + 1, 
                                     x, 
                                     sum + i, 
                                     k)
			
			i += 1
		
	
	def combination(self, x, k) :
		if (k <= 0) :
			return
		
		print("\n Given x : ", x ," k : ", k ," \n ", end = "")
		#  Collect result
		result = [0] * (k)
		#  Test
		self.findCombination(result, 0, x, 0, k)
	

def main() :
	task = DigitSum()
	#  Test A
	#  Given x : 5  k : 3 
	#  104
	#  113
	#  122
	#  131
	#  140
	#  203
	#  212
	#  221
	#  230
	#  302
	#  311
	#  320
	#  401
	#  410
	#  500
	#  -----------
	#  Note [050 , 005 , 041,.....] 
	#  Etc are not valid decimal number
	task.combination(5, 3)
	#  Test B
	#  x = 6 sum
	#  k = 4 length
	# -----------------
	task.combination(6, 4)

if __name__ == "__main__": main()

Output

 Given x :  5  k :  3
 104
 113
 122
 131
 140
 203
 212
 221
 230
 302
 311
 320
 401
 410
 500

 Given x :  6  k :  4
 1005
 1014
 1023
 1032
 1041
 1050
 1104
 1113
 1122
 1131
 1140
 1203
 1212
 1221
 1230
 1302
 1311
 1320
 1401
 1410
 1500
 2004
 2013
 2022
 2031
 2040
 2103
 2112
 2121
 2130
 2202
 2211
 2220
 2301
 2310
 2400
 3003
 3012
 3021
 3030
 3102
 3111
 3120
 3201
 3210
 3300
 4002
 4011
 4020
 4101
 4110
 4200
 5001
 5010
 5100
 6000
#  Ruby Program 
#  Generate all k digit number whose sum is to x
class DigitSum 
	#  Display result
	def printSequence(result, k) 
		i = 0
		while (i < k) 
			print(result[i])
			i += 1
		end

		print("\n ")
	end

	def findCombination(result, index, x, sum, k) 
		if (index == k && x == sum) 
			#  Display calculated result
			self.printSequence(result, index)
			return
		end

		if (sum > x || index >= k) 
			#  Base case when stop process
			return
		end

		i = 0
		while (i <= 9 && sum + i <= x) 
			if (!(index == 0 && i == 0)) 
				#  Collects resultant value
				result[index] = i
				#  Find next sequence
				self.findCombination(result, 
                                     index + 1, 
                                     x, 
                                     sum + i, 
                                     k)
			end

			i += 1
		end

	end

	def combination(x, k) 
		if (k <= 0) 
			return
		end

		print("\n Given x : ", x ," k : ", k ," \n ")
		#  Collect result
		result = Array.new(k) {0}
		#  Test
		self.findCombination(result, 0, x, 0, k)
	end

end

def main() 
	task = DigitSum.new()
	#  Test A
	#  Given x : 5  k : 3 
	#  104
	#  113
	#  122
	#  131
	#  140
	#  203
	#  212
	#  221
	#  230
	#  302
	#  311
	#  320
	#  401
	#  410
	#  500
	#  -----------
	#  Note [050 , 005 , 041,.....] 
	#  Etc are not valid decimal number
	task.combination(5, 3)
	#  Test B
	#  x = 6 sum
	#  k = 4 length
	# -----------------
	task.combination(6, 4)
end

main()

Output

 Given x : 5 k : 3 
 104
 113
 122
 131
 140
 203
 212
 221
 230
 302
 311
 320
 401
 410
 500
 
 Given x : 6 k : 4 
 1005
 1014
 1023
 1032
 1041
 1050
 1104
 1113
 1122
 1131
 1140
 1203
 1212
 1221
 1230
 1302
 1311
 1320
 1401
 1410
 1500
 2004
 2013
 2022
 2031
 2040
 2103
 2112
 2121
 2130
 2202
 2211
 2220
 2301
 2310
 2400
 3003
 3012
 3021
 3030
 3102
 3111
 3120
 3201
 3210
 3300
 4002
 4011
 4020
 4101
 4110
 4200
 5001
 5010
 5100
 6000
 
// Scala Program 
// Generate all k digit number whose sum is to x
class DigitSum()
{
	// Display result
	def printSequence(result: Array[Int], k: Int): Unit = {
		var i: Int = 0;
		while (i < k)
		{
			print(result(i));
			i += 1;
		}
		print("\n ");
	}
	def findCombination(result: Array[Int], 
      index: Int, 
        x: Int, 
          sum: Int, 
            k: Int): Unit = {
		if (index == k && x == sum)
		{
			// Display calculated result
			printSequence(result, index);
			return;
		}
		if (sum > x || index >= k)
		{
			// Base case when stop process
			return;
		}
		var i: Int = 0;
		while (i <= 9 && sum + i <= x)
		{
			if (!(index == 0 && i == 0))
			{
				// Collects resultant value
				result(index) = i;
				// Find next sequence
				findCombination(result, index + 1, x, sum + i, k);
			}
			i += 1;
		}
	}
	def combination(x: Int, k: Int): Unit = {
		if (k <= 0)
		{
			return;
		}
		print("\n Given x : " + x + " k : " + k + " \n ");
		// Collect result
		var result: Array[Int] = Array.fill[Int](k)(0);
		// Test
		findCombination(result, 0, x, 0, k);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: DigitSum = new DigitSum();
		// Test A
		// Given x : 5  k : 3 
		// 104
		// 113
		// 122
		// 131
		// 140
		// 203
		// 212
		// 221
		// 230
		// 302
		// 311
		// 320
		// 401
		// 410
		// 500
		// -----------
		// Note [050 , 005 , 041,.....] 
		// Etc are not valid decimal number
		task.combination(5, 3);
		// Test B
		// x = 6 sum
		// k = 4 length
		//-----------------
		task.combination(6, 4);
	}
}

Output

 Given x : 5 k : 3
 104
 113
 122
 131
 140
 203
 212
 221
 230
 302
 311
 320
 401
 410
 500

 Given x : 6 k : 4
 1005
 1014
 1023
 1032
 1041
 1050
 1104
 1113
 1122
 1131
 1140
 1203
 1212
 1221
 1230
 1302
 1311
 1320
 1401
 1410
 1500
 2004
 2013
 2022
 2031
 2040
 2103
 2112
 2121
 2130
 2202
 2211
 2220
 2301
 2310
 2400
 3003
 3012
 3021
 3030
 3102
 3111
 3120
 3201
 3210
 3300
 4002
 4011
 4020
 4101
 4110
 4200
 5001
 5010
 5100
 6000
// Swift 4 Program 
// Generate all k digit number whose sum is to x
class DigitSum
{
	// Display result
	func printSequence(_ result: [Int], _ k: Int)
	{
		var i: Int = 0;
		while (i < k)
		{
			print(result[i], terminator: "");
			i += 1;
		}
		print("\n ", terminator: "");
	}
	func findCombination(_ result: inout[Int], 
      _ index: Int, 
        _ x: Int, 
          _ sum: Int, 
            _ k: Int)
	{
		if (index == k && x == sum)
		{
			// Display calculated result
			self.printSequence(result, index);
			return;
		}
		if (sum > x || index >= k)
		{
			// Base case when stop process
			return;
		}
		var i: Int = 0;
		while (i <= 9 && sum + i <= x)
		{
			if (!(index == 0 && i == 0))
			{
				// Collects resultant value
				result[index] = i;
				// Find next sequence
				self.findCombination(&result, index + 1, x, sum + i, k);
			}
			i += 1;
		}
	}
	func combination(_ x: Int, _ k: Int)
	{
		if (k <= 0)
		{
			return;
		}
		print("\n Given x : ", x ," k : ", k ," \n ", terminator: "");
		// Collect result
		var result: [Int] = Array(repeating: 0, count: k);
		// Test
		self.findCombination(&result, 0, x, 0, k);
	}
}
func main()
{
	let task: DigitSum = DigitSum();
	// Test A
	// Given x : 5  k : 3 
	// 104
	// 113
	// 122
	// 131
	// 140
	// 203
	// 212
	// 221
	// 230
	// 302
	// 311
	// 320
	// 401
	// 410
	// 500
	// -----------
	// Note [050 , 005 , 041,.....] 
	// Etc are not valid decimal number
	task.combination(5, 3);
	// Test B
	// x = 6 sum
	// k = 4 length
	//-----------------
	task.combination(6, 4);
}
main();

Output

 Given x :  5  k :  3
 104
 113
 122
 131
 140
 203
 212
 221
 230
 302
 311
 320
 401
 410
 500

 Given x :  6  k :  4
 1005
 1014
 1023
 1032
 1041
 1050
 1104
 1113
 1122
 1131
 1140
 1203
 1212
 1221
 1230
 1302
 1311
 1320
 1401
 1410
 1500
 2004
 2013
 2022
 2031
 2040
 2103
 2112
 2121
 2130
 2202
 2211
 2220
 2301
 2310
 2400
 3003
 3012
 3021
 3030
 3102
 3111
 3120
 3201
 3210
 3300
 4002
 4011
 4020
 4101
 4110
 4200
 5001
 5010
 5100
 6000
// Kotlin Program 
// Generate all k digit number whose sum is to x
class DigitSum
{
	// Display result
	fun printSequence(result: Array < Int > , 
                       k: Int): Unit
	{
		var i: Int = 0;
		while (i < k)
		{
			print(result[i]);
			i += 1;
		}
		print("\n ");
	}
	fun findCombination(result: Array < Int > , 
                        index: Int, 
                        x: Int, 
                        sum: Int, 
                        k: Int): Unit
	{
		if (index == k && x == sum)
		{
			// Display calculated result
			this.printSequence(result, index);
			return;
		}
		if (sum > x || index >= k)
		{
			// Base case when stop process
			return;
		}
		var i: Int = 0;
		while (i <= 9 && sum + i <= x)
		{
			if (!(index == 0 && i == 0))
			{
				// Collects resultant value
				result[index] = i;
				// Find next sequence
				this.findCombination(result, 
                                     index + 1, 
                                     x, 
                                     sum + i, 
                                     k);
			}
			i += 1;
		}
	}
	fun combination(x: Int, k: Int): Unit
	{
		if (k <= 0)
		{
			return;
		}
		print("\n Given x : " + x + " k : " + k + " \n ");
		// Collect result
		val result: Array < Int > = Array(k)
		{
			0
		};
		// Test
		this.findCombination(result, 0, x, 0, k);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: DigitSum = DigitSum();
	// Test A
	// Given x : 5  k : 3 
	// 104
	// 113
	// 122
	// 131
	// 140
	// 203
	// 212
	// 221
	// 230
	// 302
	// 311
	// 320
	// 401
	// 410
	// 500
	// -----------
	// Note [050 , 005 , 041,.....] 
	// Etc are not valid decimal number
	task.combination(5, 3);
	// Test B
	// x = 6 sum
	// k = 4 length
	//-----------------
	task.combination(6, 4);
}

Output

 Given x : 5 k : 3
 104
 113
 122
 131
 140
 203
 212
 221
 230
 302
 311
 320
 401
 410
 500

 Given x : 6 k : 4
 1005
 1014
 1023
 1032
 1041
 1050
 1104
 1113
 1122
 1131
 1140
 1203
 1212
 1221
 1230
 1302
 1311
 1320
 1401
 1410
 1500
 2004
 2013
 2022
 2031
 2040
 2103
 2112
 2121
 2130
 2202
 2211
 2220
 2301
 2310
 2400
 3003
 3012
 3021
 3030
 3102
 3111
 3120
 3201
 3210
 3300
 4002
 4011
 4020
 4101
 4110
 4200
 5001
 5010
 5100
 6000


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