Print all combinations factors of given number

Here given code implementation process.

//  C program for
//  Print all combinations factors of given number
#include <stdio.h>

//  Print resultant element
void printSequence(int result[], int n)
{
	for (int i = 0; i < n; ++i)
	{
		printf("  %d", result[i]);
	}
	printf("\n");
}
void findCombination(int result[], int index, int count, int num)
{
	if (num == count)
	{
		printSequence(result, index);
		return;
	}
	if (index > num || count > num)
	{
		// Base case
		return;
	}
	for (int i = 2; i <= num / 2; ++i)
	{
		// Collect resultant element
		result[index] = i;
		// Find combinations using recursively
		findCombination(result, index + 1, count *i, num);
	}
}
void combination(int num)
{
	if (num <= 0)
	{
		return;
	}
	printf("\n Given Number : %d\n", num);
	if (num == 1)
	{
		// When have only one result
		printf("\n 1 \n");
		return;
	}
	// Result container
	int result[num];
	// Find combination pair
	findCombination(result, 0, 1, num);
}
int main(int argc, char
	const *argv[])
{
	combination(24);
	return 0;
}

Output

 Given Number : 24
  2  2  2  3
  2  2  3  2
  2  2  6
  2  3  2  2
  2  3  4
  2  4  3
  2  6  2
  2  12
  3  2  2  2
  3  2  4
  3  4  2
  3  8
  4  2  3
  4  3  2
  4  6
  6  2  2
  6  4
  8  3
  12  2
/*
    Java Program
    Print all combinations factors of given number
*/
public class Combinations
{
	//  Print resultant element
	public void printSequence(int[] result, int n)
	{
		for (int i = 0; i < n; ++i)
		{
			System.out.print(" " + result[i]);
		}
		System.out.print("\n");
	}
	public void findCombination(int[] result, 
                                int index, 
                                int count, 
                                int num)
	{
		if (num == count)
		{
			printSequence(result, index);
			return;
		}
		if (index > num || count > num)
		{
			// Base case
			return;
		}
		for (int i = 2; i <= num / 2; ++i)
		{
			// Collect resultant element
			result[index] = i;
			// Find combinations using recursively
			findCombination(result, index + 1, count * i, num);
		}
	}
	public void combination(int num)
	{
		if (num <= 0)
		{
			return;
		}
		System.out.print("\n Given Number : " + num + "\n");
		if (num == 1)
		{
			// When have only one result
			System.out.print("\n 1 \n");
			return;
		}
		// Result container
		int[] result = new int[num];
		// Find combination pair
		findCombination(result, 0, 1, num);
	}
	public static void main(String[] args)
	{
		Combinations task = new Combinations();
		// Test
		task.combination(24);
	}
}

Output

 Given Number : 24
 2 2 2 3
 2 2 3 2
 2 2 6
 2 3 2 2
 2 3 4
 2 4 3
 2 6 2
 2 12
 3 2 2 2
 3 2 4
 3 4 2
 3 8
 4 2 3
 4 3 2
 4 6
 6 2 2
 6 4
 8 3
 12 2
// Include header file
#include <iostream>
using namespace std;
/*
    C++ Program
    Print all combinations factors of given number
*/
class Combinations
{
	public:
		//  Print resultant element
		void printSequence(int result[], int n)
		{
			for (int i = 0; i < n; ++i)
			{
				cout << " " << result[i];
			}
			cout << "\n";
		}
	void findCombination(int result[], 
      int index, 
        int count, 
          int num)
	{
		if (num == count)
		{
			this->printSequence(result, index);
			return;
		}
		if (index > num || count > num)
		{
			// Base case
			return;
		}
		for (int i = 2; i <= num / 2; ++i)
		{
			// Collect resultant element
			result[index] = i;
			// Find combinations using recursively
			this->findCombination(result, 
                                  index + 1, 
                                  count *i, 
                                  num);
		}
	}
	void combination(int num)
	{
		if (num <= 0)
		{
			return;
		}
		cout << "\n Given Number : " << num << "\n";
		if (num == 1)
		{
			// When have only one result
			cout << "\n 1 \n";
			return;
		}
		// Result container
		int result[num];
		// Find combination pair
		this->findCombination(result, 0, 1, num);
	}
};
int main()
{
	Combinations *task = new Combinations();
	// Test
	task->combination(24);
	return 0;
}

Output

 Given Number : 24
 2 2 2 3
 2 2 3 2
 2 2 6
 2 3 2 2
 2 3 4
 2 4 3
 2 6 2
 2 12
 3 2 2 2
 3 2 4
 3 4 2
 3 8
 4 2 3
 4 3 2
 4 6
 6 2 2
 6 4
 8 3
 12 2
// Include namespace system
using System;
/*
    Csharp Program
    Print all combinations factors of given number
*/
public class Combinations
{
	//  Print resultant element
	public void printSequence(int[] result, int n)
	{
		for (int i = 0; i < n; ++i)
		{
			Console.Write(" " + result[i]);
		}
		Console.Write("\n");
	}
	public void findCombination(int[] result, 
                                int index, 
                                int count, 
                                int num)
	{
		if (num == count)
		{
			this.printSequence(result, index);
			return;
		}
		if (index > num || count > num)
		{
			// Base case
			return;
		}
		for (int i = 2; i <= num / 2; ++i)
		{
			// Collect resultant element
			result[index] = i;
			// Find combinations using recursively
			this.findCombination(result, 
                                 index + 1, 
                                 count * i, 
                                 num);
		}
	}
	public void combination(int num)
	{
		if (num <= 0)
		{
			return;
		}
		Console.Write("\n Given Number : " + num + "\n");
		if (num == 1)
		{
			// When have only one result
			Console.Write("\n 1 \n");
			return;
		}
		// Result container
		int[] result = new int[num];
		// Find combination pair
		this.findCombination(result, 0, 1, num);
	}
	public static void Main(String[] args)
	{
		Combinations task = new Combinations();
		// Test
		task.combination(24);
	}
}

Output

 Given Number : 24
 2 2 2 3
 2 2 3 2
 2 2 6
 2 3 2 2
 2 3 4
 2 4 3
 2 6 2
 2 12
 3 2 2 2
 3 2 4
 3 4 2
 3 8
 4 2 3
 4 3 2
 4 6
 6 2 2
 6 4
 8 3
 12 2
package main
import "fmt"
/*
    Go Program
    Print all combinations factors of given number
*/
type Combinations struct {}
func getCombinations() * Combinations {
	var me *Combinations = &Combinations {}
	return me
}
//  Print resultant element
func(this Combinations) printSequence(result[] int, n int) {
	for i := 0 ; i < n ; i++ {
		fmt.Print(" ", result[i])
	}
	fmt.Print("\n")
}
func(this Combinations) findCombination(result[] int, 
	index int, count int, num int) {
	if num == count {
		this.printSequence(result, index)
		return
	}
	if index > num || count > num {
		// Base case
		return
	}
	for i := 2 ; i <= num / 2 ; i++ {
		// Collect resultant element
		result[index] = i
		// Find combinations using recursively
		this.findCombination(result, index + 1, count * i, num)
	}
}
func(this Combinations) combination(num int) {
	if num <= 0 {
		return
	}
	fmt.Print("\n Given Number : ", num, "\n")
	if num == 1 {
		// When have only one result
		fmt.Print("\n 1 \n")
		return
	}
	// Result container
	var result = make([] int, num)
	// Find combination pair
	this.findCombination(result, 0, 1, num)
}
func main() {
	var task * Combinations = getCombinations()
	// Test
	task.combination(24)
}

Output

 Given Number : 24
 2 2 2 3
 2 2 3 2
 2 2 6
 2 3 2 2
 2 3 4
 2 4 3
 2 6 2
 2 12
 3 2 2 2
 3 2 4
 3 4 2
 3 8
 4 2 3
 4 3 2
 4 6
 6 2 2
 6 4
 8 3
 12 2
<?php
/*
    Php Program
    Print all combinations factors of given number
*/
class Combinations
{
	//  Print resultant element
	public	function printSequence($result, $n)
	{
		for ($i = 0; $i < $n; ++$i)
		{
			echo(" ".$result[$i]);
		}
		echo("\n");
	}
	public	function findCombination($result, $index, $count, $num)
	{
		if ($num == $count)
		{
			$this->printSequence($result, $index);
			return;
		}
		if ($index > $num || $count > $num)
		{
			// Base case
			return;
		}
		for ($i = 2; $i <= (int)($num / 2); ++$i)
		{
			// Collect resultant element
			$result[$index] = $i;
			// Find combinations using recursively
			$this->findCombination($result, 
                                   $index + 1, 
                                   $count * $i, 
                                   $num);
		}
	}
	public	function combination($num)
	{
		if ($num <= 0)
		{
			return;
		}
		echo("\n Given Number : ".$num.
			"\n");
		if ($num == 1)
		{
			// When have only one result
			echo("\n 1 \n");
			return;
		}
		// Result container
		$result = array_fill(0, $num, 0);
		// Find combination pair
		$this->findCombination($result, 0, 1, $num);
	}
}

function main()
{
	$task = new Combinations();
	// Test
	$task->combination(24);
}
main();

Output

 Given Number : 24
 2 2 2 3
 2 2 3 2
 2 2 6
 2 3 2 2
 2 3 4
 2 4 3
 2 6 2
 2 12
 3 2 2 2
 3 2 4
 3 4 2
 3 8
 4 2 3
 4 3 2
 4 6
 6 2 2
 6 4
 8 3
 12 2
/*
    Node JS Program
    Print all combinations factors of given number
*/
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(result, index, count, num)
	{
		if (num == count)
		{
			this.printSequence(result, index);
			return;
		}
		if (index > num || count > num)
		{
			// Base case
			return;
		}
		for (var i = 2; i <= parseInt(num / 2); ++i)
		{
			// Collect resultant element
			result[index] = i;
			// Find combinations using recursively
			this.findCombination(result, index + 1, count * i, num);
		}
	}
	combination(num)
	{
		if (num <= 0)
		{
			return;
		}
		process.stdout.write("\n Given Number : " + num + "\n");
		if (num == 1)
		{
			// When have only one result
			process.stdout.write("\n 1 \n");
			return;
		}
		// Result container
		var result = Array(num).fill(0);
		// Find combination pair
		this.findCombination(result, 0, 1, num);
	}
}

function main()
{
	var task = new Combinations();
	// Test
	task.combination(24);
}
main();

Output

 Given Number : 24
 2 2 2 3
 2 2 3 2
 2 2 6
 2 3 2 2
 2 3 4
 2 4 3
 2 6 2
 2 12
 3 2 2 2
 3 2 4
 3 4 2
 3 8
 4 2 3
 4 3 2
 4 6
 6 2 2
 6 4
 8 3
 12 2
#    Python 3 Program
#    Print all combinations factors of given number
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, result, index, count, num) :
		if (num == count) :
			self.printSequence(result, index)
			return
		
		if (index > num or count > num) :
			#  Base case
			return
		
		i = 2
		while (i <= int(num / 2)) :
			#  Collect resultant element
			result[index] = i
			#  Find combinations using recursively
			self.findCombination(result, index + 1, count * i, num)
			i += 1
		
	
	def combination(self, num) :
		if (num <= 0) :
			return
		
		print("\n Given Number : ", num )
		if (num == 1) :
			#  When have only one result
			print("\n 1 ")
			return
		
		#  Result container
		result = [0] * (num)
		#  Find combination pair
		self.findCombination(result, 0, 1, num)
	

def main() :
	task = Combinations()
	#  Test
	task.combination(24)

if __name__ == "__main__": main()

Output

 Given Number :  24
  2  2  2  3
  2  2  3  2
  2  2  6
  2  3  2  2
  2  3  4
  2  4  3
  2  6  2
  2  12
  3  2  2  2
  3  2  4
  3  4  2
  3  8
  4  2  3
  4  3  2
  4  6
  6  2  2
  6  4
  8  3
  12  2
#    Ruby Program
#    Print all combinations factors of given number
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(result, index, count, num) 
		if (num == count) 
			self.printSequence(result, index)
			return
		end

		if (index > num || count > num) 
			#  Base case
			return
		end

		i = 2
		while (i <= num / 2) 
			#  Collect resultant element
			result[index] = i
			#  Find combinations using recursively
			self.findCombination(result, index + 1, count * i, num)
			i += 1
		end

	end

	def combination(num) 
		if (num <= 0) 
			return
		end

		print("\n Given Number : ", num ,"\n")
		if (num == 1) 
			#  When have only one result
			print("\n 1 \n")
			return
		end

		#  Result container
		result = Array.new(num) {0}
		#  Find combination pair
		self.findCombination(result, 0, 1, num)
	end

end

def main() 
	task = Combinations.new()
	#  Test
	task.combination(24)
end

main()

Output

 Given Number : 24
 2 2 2 3
 2 2 3 2
 2 2 6
 2 3 2 2
 2 3 4
 2 4 3
 2 6 2
 2 12
 3 2 2 2
 3 2 4
 3 4 2
 3 8
 4 2 3
 4 3 2
 4 6
 6 2 2
 6 4
 8 3
 12 2
/*
    Scala Program
    Print all combinations factors of given number
*/
class Combinations()
{
	//  Print resultant element
	def printSequence(result: Array[Int], n: Int): Unit = {
		var i: Int = 0;
		while (i < n)
		{
			print(" " + result(i));
			i += 1;
		}
		print("\n");
	}
	def findCombination(result: Array[Int], index: Int, 
      count: Int, num: Int): Unit = {
		if (num == count)
		{
			printSequence(result, index);
			return;
		}
		if (index > num || count > num)
		{
			// Base case
			return;
		}
		var i: Int = 2;
		while (i <= num / 2)
		{
			// Collect resultant element
			result(index) = i;
			// Find combinations using recursively
			findCombination(result, index + 1, count * i, num);
			i += 1;
		}
	}
	def combination(num: Int): Unit = {
		if (num <= 0)
		{
			return;
		}
		print("\n Given Number : " + num + "\n");
		if (num == 1)
		{
			// When have only one result
			print("\n 1 \n");
			return;
		}
		// Result container
		var result: Array[Int] = Array.fill[Int](num)(0);
		// Find combination pair
		findCombination(result, 0, 1, num);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Combinations = new Combinations();
		// Test
		task.combination(24);
	}
}

Output

 Given Number : 24
 2 2 2 3
 2 2 3 2
 2 2 6
 2 3 2 2
 2 3 4
 2 4 3
 2 6 2
 2 12
 3 2 2 2
 3 2 4
 3 4 2
 3 8
 4 2 3
 4 3 2
 4 6
 6 2 2
 6 4
 8 3
 12 2
/*
    Swift 4 Program
    Print all combinations factors of given number
*/
class Combinations
{
	//  Print resultant element
	func printSequence(_ result: [Int], _ n: Int)
	{
		var i: Int = 0;
		while (i < n)
		{
			print(" ", result[i], terminator: "");
			i += 1;
		}
		print(terminator: "\n");
	}
	func findCombination(_ result: inout[Int], _ index: Int, 
      _ count: Int, _ num: Int)
	{
		if (num == count)
		{
			self.printSequence(result, index);
			return;
		}
		if (index > num || count > num)
		{
			// Base case
			return;
		}
		var i: Int = 2;
		while (i <= num / 2)
		{
			// Collect resultant element
			result[index] = i;
			// Find combinations using recursively
			self.findCombination(&result, index + 1, count * i, num);
			i += 1;
		}
	}
	func combination(_ num: Int)
	{
		if (num <= 0)
		{
			return;
		}
		print("\n Given Number : ", num );
		if (num == 1)
		{
			// When have only one result
			print("\n 1 ");
			return;
		}
		// Result container
		var result: [Int] = Array(repeating: 0, count: num);
		// Find combination pair
		self.findCombination(&result, 0, 1, num);
	}
}
func main()
{
	let task: Combinations = Combinations();
	// Test
	task.combination(24);
}
main();

Output

 Given Number :  24
  2  2  2  3
  2  2  3  2
  2  2  6
  2  3  2  2
  2  3  4
  2  4  3
  2  6  2
  2  12
  3  2  2  2
  3  2  4
  3  4  2
  3  8
  4  2  3
  4  3  2
  4  6
  6  2  2
  6  4
  8  3
  12  2
/*
    Kotlin Program
    Print all combinations factors of given number
*/
class Combinations
{
	//  Print resultant element
	fun printSequence(result: Array < Int > , n: Int): Unit
	{
		var i: Int = 0;
		while (i < n)
		{
			print(" " + result[i]);
			i += 1;
		}
		print("\n");
	}
	fun findCombination(result: Array < Int > , 
                        index: Int, 
                        count: Int, 
                        num: Int): Unit
	{
		if (num == count)
		{
			this.printSequence(result, index);
			return;
		}
		if (index > num || count > num)
		{
			// Base case
			return;
		}
		var i: Int = 2;
		while (i <= num / 2)
		{
			// Collect resultant element
			result[index] = i;
			// Find combinations using recursively
			this.findCombination(result, index + 1, count * i, num);
			i += 1;
		}
	}
	fun combination(num: Int): Unit
	{
		if (num <= 0)
		{
			return;
		}
		print("\n Given Number : " + num + "\n");
		if (num == 1)
		{
			// When have only one result
			print("\n 1 \n");
			return;
		}
		// Result container
		val result: Array < Int > = Array(num)
		{
			0
		};
		// Find combination pair
		this.findCombination(result, 0, 1, num);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Combinations = Combinations();
	// Test
	task.combination(24);
}

Output

 Given Number : 24
 2 2 2 3
 2 2 3 2
 2 2 6
 2 3 2 2
 2 3 4
 2 4 3
 2 6 2
 2 12
 3 2 2 2
 3 2 4
 3 4 2
 3 8
 4 2 3
 4 3 2
 4 6
 6 2 2
 6 4
 8 3
 12 2


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