# Generate all the products of a number in which each element is divisible by the previous element

Here given code implementation process.

// C Program
// Generate all the products of a number in which
// each element is divisible by the previous element
#include <stdio.h>

// Display result
void printSequence(int result[], int n)
{
for (int i = 0; i < n; ++i)
{
printf("  %d", result[i]);
}
printf("\n");
}
void findCombination(int num, int result[], int index, int product)
{
if (product == num)
{
// Display calculated result
printSequence(result, index);
return;
}
if (index >= num || product > num)
{
// Base case when stop process
return;
}
for (int i = 2; i <= num / 2; ++i)
{
if (index == 0 || i % result[index - 1] == 0)
{
// Collects resultant value
result[index] = i;
// Find other combination using recursion
findCombination(num, result, index + 1, product *i);
}
}
}
void combination(int num)
{
if (num <= 0)
{
return;
}
if (num == 1)
{
printf("\n 1 \n");
return;
}
printf("\n Given Number : %d\n", num);
// Collect result
int result[num];
// Test
findCombination(num, result, 0, 1);
}
int main()
{
int num = 56;
/*
num = 56
--------
2 ⤌ 2 ⤌ 14
2 ⤌ 28
---------------
Here  2  % 2  == 0   14 % 2 == 0
28 % 2 == 0
Element divisible previous element
*/
combination(num);
num = 32;
/*
num = 32
---------------
2 ⤌ 2 ⤌ 2 ⤌ 2 ⤌ 2
2 ⤌ 2 ⤌ 2 ⤌ 4
2 ⤌ 2 ⤌ 8
2 ⤌ 4 ⤌ 4
2 ⤌ 16
4 ⤌ 8

*/
combination(num);
num = 11;
/*
num = 11
---------------
None

*/
combination(num);
return 0;
}

#### Output

Given Number : 56
2  2  14
2  28

Given Number : 32
2  2  2  2  2
2  2  2  4
2  2  8
2  4  4
2  16
4  8

Given Number : 11
// Java Program
// Generate all the products of a number in which
// each element is divisible by the previous element
public class Combinations
{
// Display result
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 num,
int[] result,
int index,
int product)
{
if (product == num)
{
// Display calculated result
printSequence(result, index);
return;
}
if (index >= num || product > num)
{
// Base case when stop process
return;
}
for (int i = 2; i <= num / 2; ++i)
{
if (index == 0 || i % result[index - 1] == 0)
{
// Collects resultant value
result[index] = i;
// Find other combination using recursion
findCombination(num, result, index + 1, product * i);
}
}
}
public void combination(int num)
{
if (num <= 0)
{
return;
}
if (num == 1)
{
System.out.print("\n 1 \n");
return;
}
System.out.println("\n Given Number : " + num);
// Collect result
int[] result = new int[num];
// Test
findCombination(num, result, 0, 1);
}
public static void main(String args[])
{
int num = 56;
/*
num = 56
--------
2 ⤌ 2 ⤌ 14
2 ⤌ 28
---------------
Here  2  % 2  == 0   14 % 2 == 0
28 % 2 == 0
Element divisible previous element
*/
num = 32;
/*
num = 32
---------------
2 ⤌ 2 ⤌ 2 ⤌ 2 ⤌ 2
2 ⤌ 2 ⤌ 2 ⤌ 4
2 ⤌ 2 ⤌ 8
2 ⤌ 4 ⤌ 4
2 ⤌ 16
4 ⤌ 8
*/
num = 11;
/*
num = 11
---------------
None
*/
}
}

#### Output

Given Number : 56
2 2 14
2 28

Given Number : 32
2 2 2 2 2
2 2 2 4
2 2 8
2 4 4
2 16
4 8

Given Number : 11
#include <iostream>
using namespace std;
// C++ Program
// Generate all the products of a number in which
// each element is divisible by the previous element
class Combinations
{
public:
// Display result
void printSequence(int result[], int n)
{
for (int i = 0; i < n; ++i)
{
cout << " " << result[i];
}
cout << "\n";
}
void findCombination(int num,
int result[],
int index,
int product)
{
if (product == num)
{
// Display calculated result
this->printSequence(result, index);
return;
}
if (index >= num || product > num)
{
// Base case when stop process
return;
}
for (int i = 2; i <= num / 2; ++i)
{
if (index == 0 || i % result[index - 1] == 0)
{
// Collects resultant value
result[index] = i;
// Find other combination using recursion
this->findCombination(num,
result,
index + 1,
product *i);
}
}
}
void combination(int num)
{
if (num <= 0)
{
return;
}
if (num == 1)
{
cout << "\n 1 \n";
return;
}
cout << "\n Given Number : " << num << endl;
// Collect result
int result[num];
// Test
this->findCombination(num, result, 0, 1);
}
};
int main()
{
int num = 56;
/*
num = 56
--------
2 ⤌ 2 ⤌ 14
2 ⤌ 28
---------------
Here  2  % 2  == 0   14 % 2 == 0
28 % 2 == 0
Element divisible previous element
*/
num = 32;
/*
num = 32
---------------
2 ⤌ 2 ⤌ 2 ⤌ 2 ⤌ 2
2 ⤌ 2 ⤌ 2 ⤌ 4
2 ⤌ 2 ⤌ 8
2 ⤌ 4 ⤌ 4
2 ⤌ 16
4 ⤌ 8
*/
num = 11;
/*
num = 11
---------------
None
*/
return 0;
}

#### Output

Given Number : 56
2 2 14
2 28

Given Number : 32
2 2 2 2 2
2 2 2 4
2 2 8
2 4 4
2 16
4 8

Given Number : 11
// Include namespace system
using System;
// Csharp Program
// Generate all the products of a number in which
// each element is divisible by the previous element
public class Combinations
{
// Display result
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 num,
int[] result,
int index,
int product)
{
if (product == num)
{
// Display calculated result
this.printSequence(result, index);
return;
}
if (index >= num || product > num)
{
// Base case when stop process
return;
}
for (int i = 2; i <= num / 2; ++i)
{
if (index == 0 || i % result[index - 1] == 0)
{
// Collects resultant value
result[index] = i;
// Find other combination using recursion
this.findCombination(num,
result,
index + 1,
product * i);
}
}
}
public void combination(int num)
{
if (num <= 0)
{
return;
}
if (num == 1)
{
Console.Write("\n 1 \n");
return;
}
Console.WriteLine("\n Given Number : " + num);
// Collect result
int[] result = new int[num];
// Test
this.findCombination(num, result, 0, 1);
}
public static void Main(String[] args)
{
int num = 56;
/*
num = 56
--------
2 ⤌ 2 ⤌ 14
2 ⤌ 28
---------------
Here  2  % 2  == 0   14 % 2 == 0
28 % 2 == 0
Element divisible previous element
*/
num = 32;
/*
num = 32
---------------
2 ⤌ 2 ⤌ 2 ⤌ 2 ⤌ 2
2 ⤌ 2 ⤌ 2 ⤌ 4
2 ⤌ 2 ⤌ 8
2 ⤌ 4 ⤌ 4
2 ⤌ 16
4 ⤌ 8
*/
num = 11;
/*
num = 11
---------------
None
*/
}
}

#### Output

Given Number : 56
2 2 14
2 28

Given Number : 32
2 2 2 2 2
2 2 2 4
2 2 8
2 4 4
2 16
4 8

Given Number : 11
package main
import "fmt"
// Go Program
// Generate all the products of a number in which
// each element is divisible by the previous element
type Combinations struct {}
func getCombinations() * Combinations {
var me *Combinations = &Combinations {}
return me
}
// Display result
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(num int,
result[] int, index int, product int) {
if product == num {
// Display calculated result
this.printSequence(result, index)
return
}
if index >= num || product > num {
// Base case when stop process
return
}
for i := 2 ; i <= num / 2 ; i++ {
if index == 0 || i % result[index - 1] == 0 {
// Collects resultant value
result[index] = i
// Find other combination using recursion
this.findCombination(num, result, index + 1, product * i)
}
}
}
func(this Combinations) combination(num int) {
if num <= 0 {
return
}
if num == 1 {
fmt.Print("\n 1 \n")
return
}
fmt.Println("\n Given Number : ", num)
// Collect result
var result = make([] int, num)
// Test
this.findCombination(num, result, 0, 1)
}
func main() {
var task * Combinations = getCombinations()
var num int = 56
/*
num = 56
--------
2 ⤌ 2 ⤌ 14
2 ⤌ 28
---------------
Here  2  % 2  == 0   14 % 2 == 0
28 % 2 == 0
Element divisible previous element
*/
num = 32
/*
num = 32
---------------
2 ⤌ 2 ⤌ 2 ⤌ 2 ⤌ 2
2 ⤌ 2 ⤌ 2 ⤌ 4
2 ⤌ 2 ⤌ 8
2 ⤌ 4 ⤌ 4
2 ⤌ 16
4 ⤌ 8
*/
num = 11
/*
num = 11
---------------
None
*/
}

#### Output

Given Number : 56
2 2 14
2 28

Given Number : 32
2 2 2 2 2
2 2 2 4
2 2 8
2 4 4
2 16
4 8

Given Number : 11
<?php
// Php Program
// Generate all the products of a number in which
// each element is divisible by the previous element
class Combinations
{
// Display result
public	function printSequence(\$result, \$n)
{
for (\$i = 0; \$i < \$n; ++\$i)
{
echo(" ".\$result[\$i]);
}
echo("\n");
}
public	function findCombination(\$num, \$result,
\$index, \$product)
{
if (\$product == \$num)
{
// Display calculated result
\$this->printSequence(\$result, \$index);
return;
}
if (\$index >= \$num || \$product > \$num)
{
// Base case when stop process
return;
}
for (\$i = 2; \$i <= (int)(\$num / 2); ++\$i)
{
if (\$index == 0 || \$i % \$result[\$index - 1] == 0)
{
// Collects resultant value
\$result[\$index] = \$i;
// Find other combination using recursion
\$this->findCombination(\$num, \$result,
\$index + 1, \$product * \$i);
}
}
}
public	function combination(\$num)
{
if (\$num <= 0)
{
return;
}
if (\$num == 1)
{
echo("\n 1 \n");
return;
}
echo("\n Given Number : ".\$num.
"\n");
// Collect result
\$result = array_fill(0, \$num, 0);
// Test
\$this->findCombination(\$num, \$result, 0, 1);
}
}

function main()
{
\$num = 56;
/*
num = 56
--------
2 ⤌ 2 ⤌ 14
2 ⤌ 28
---------------
Here  2  % 2  == 0   14 % 2 == 0
28 % 2 == 0
Element divisible previous element
*/
\$num = 32;
/*
num = 32
---------------
2 ⤌ 2 ⤌ 2 ⤌ 2 ⤌ 2
2 ⤌ 2 ⤌ 2 ⤌ 4
2 ⤌ 2 ⤌ 8
2 ⤌ 4 ⤌ 4
2 ⤌ 16
4 ⤌ 8
*/
\$num = 11;
/*
num = 11
---------------
None
*/
}
main();

#### Output

Given Number : 56
2 2 14
2 28

Given Number : 32
2 2 2 2 2
2 2 2 4
2 2 8
2 4 4
2 16
4 8

Given Number : 11
// Node JS Program
// Generate all the products of a number in which
// each element is divisible by the previous element
class Combinations
{
// Display result
printSequence(result, n)
{
for (var i = 0; i < n; ++i)
{
process.stdout.write(" " + result[i]);
}
process.stdout.write("\n");
}
findCombination(num, result, index, product)
{
if (product == num)
{
// Display calculated result
this.printSequence(result, index);
return;
}
if (index >= num || product > num)
{
// Base case when stop process
return;
}
for (var i = 2; i <= parseInt(num / 2); ++i)
{
if (index == 0 || i % result[index - 1] == 0)
{
// Collects resultant value
result[index] = i;
// Find other combination using recursion
this.findCombination(num, result,
index + 1,
product * i);
}
}
}
combination(num)
{
if (num <= 0)
{
return;
}
if (num == 1)
{
process.stdout.write("\n 1 \n");
return;
}
console.log("\n Given Number : " + num);
// Collect result
var result = Array(num).fill(0);
// Test
this.findCombination(num, result, 0, 1);
}
}

function main()
{
var num = 56;
/*
num = 56
--------
2 ⤌ 2 ⤌ 14
2 ⤌ 28
---------------
Here  2  % 2  == 0   14 % 2 == 0
28 % 2 == 0
Element divisible previous element
*/
num = 32;
/*
num = 32
---------------
2 ⤌ 2 ⤌ 2 ⤌ 2 ⤌ 2
2 ⤌ 2 ⤌ 2 ⤌ 4
2 ⤌ 2 ⤌ 8
2 ⤌ 4 ⤌ 4
2 ⤌ 16
4 ⤌ 8
*/
num = 11;
/*
num = 11
---------------
None
*/
}
main();

#### Output

Given Number : 56
2 2 14
2 28

Given Number : 32
2 2 2 2 2
2 2 2 4
2 2 8
2 4 4
2 16
4 8

Given Number : 11
#  Python 3 Program
#  Generate all the products of a number in which
#  each element is divisible by the previous element
class Combinations :
#  Display result
def printSequence(self, result, n) :
i = 0
while (i < n) :
print(" ", result[i], end = "")
i += 1

print(end = "\n")

def findCombination(self, num, result, index, product) :
if (product == num) :
#  Display calculated result
self.printSequence(result, index)
return

if (index >= num or product > num) :
#  Base case when stop process
return

i = 2
while (i <= int(num / 2)) :
if (index == 0 or i % result[index - 1] == 0) :
#  Collects resultant value
result[index] = i
#  Find other combination using recursion
self.findCombination(num, result,
index + 1, product * i)

i += 1

def combination(self, num) :
if (num <= 0) :
return

if (num == 1) :
print("\n 1 ")
return

print("\n Given Number : ", num)
#  Collect result
result = [0] * (num)
#  Test
self.findCombination(num, result, 0, 1)

def main() :
num = 56
# 	num = 56
# 	--------
# 	2 ⤌ 2 ⤌ 14
# 	2 ⤌ 28
# 	---------------
# 	Here  2  % 2  == 0   14 % 2 == 0
# 	      28 % 2 == 0
# 	Element divisible previous element
num = 32
# 	num = 32
# 	---------------
# 	2 ⤌ 2 ⤌ 2 ⤌ 2 ⤌ 2
# 	2 ⤌ 2 ⤌ 2 ⤌ 4
# 	2 ⤌ 2 ⤌ 8
# 	2 ⤌ 4 ⤌ 4
# 	2 ⤌ 16
# 	4 ⤌ 8
num = 11
# 	num = 11
# 	---------------
# 	None

if __name__ == "__main__": main()

#### Output

Given Number :  56
2  2  14
2  28

Given Number :  32
2  2  2  2  2
2  2  2  4
2  2  8
2  4  4
2  16
4  8

Given Number :  11
#  Ruby Program
#  Generate all the products of a number in which
#  each element is divisible by the previous element
class Combinations
#  Display result
def printSequence(result, n)
i = 0
while (i < n)
print(" ", result[i])
i += 1
end

print("\n")
end

def findCombination(num, result, index, product)
if (product == num)
#  Display calculated result
self.printSequence(result, index)
return
end

if (index >= num || product > num)
#  Base case when stop process
return
end

i = 2
while (i <= num / 2)
if (index == 0 || i % result[index - 1] == 0)
#  Collects resultant value
result[index] = i
#  Find other combination using recursion
self.findCombination(num, result,
index + 1, product * i)
end

i += 1
end

end

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

if (num == 1)
print("\n 1 \n")
return
end

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

end

def main()
num = 56
# 	num = 56
# 	--------
# 	2 ⤌ 2 ⤌ 14
# 	2 ⤌ 28
# 	---------------
# 	Here  2  % 2  == 0   14 % 2 == 0
# 	      28 % 2 == 0
# 	Element divisible previous element
num = 32
# 	num = 32
# 	---------------
# 	2 ⤌ 2 ⤌ 2 ⤌ 2 ⤌ 2
# 	2 ⤌ 2 ⤌ 2 ⤌ 4
# 	2 ⤌ 2 ⤌ 8
# 	2 ⤌ 4 ⤌ 4
# 	2 ⤌ 16
# 	4 ⤌ 8
num = 11
# 	num = 11
# 	---------------
# 	None
end

main()

#### Output

Given Number : 56
2 2 14
2 28

Given Number : 32
2 2 2 2 2
2 2 2 4
2 2 8
2 4 4
2 16
4 8

Given Number : 11
// Scala Program
// Generate all the products of a number in which
// each element is divisible by the previous element
class Combinations()
{
// Display result
def printSequence(result: Array[Int], n: Int): Unit = {
var i: Int = 0;
while (i < n)
{
print(" " + result(i));
i += 1;
}
print("\n");
}
def findCombination(num: Int,
result: Array[Int],
index: Int,
product: Int): Unit = {
if (product == num)
{
// Display calculated result
printSequence(result, index);
return;
}
if (index >= num || product > num)
{
// Base case when stop process
return;
}
var i: Int = 2;
while (i <= num / 2)
{
if (index == 0 || i % result(index - 1) == 0)
{
// Collects resultant value
result(index) = i;
// Find other combination using recursion
findCombination(num, result, index + 1, product * i);
}
i += 1;
}
}
def combination(num: Int): Unit = {
if (num <= 0)
{
return;
}
if (num == 1)
{
print("\n 1 \n");
return;
}
println("\n Given Number : " + num);
// Collect result
var result: Array[Int] = Array.fill[Int](num)(0);
// Test
findCombination(num, result, 0, 1);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Combinations = new Combinations();
var num: Int = 56;
/*
num = 56
--------
2 ⤌ 2 ⤌ 14
2 ⤌ 28
---------------
Here  2  % 2  == 0   14 % 2 == 0
28 % 2 == 0
Element divisible previous element
*/
num = 32;
/*
num = 32
---------------
2 ⤌ 2 ⤌ 2 ⤌ 2 ⤌ 2
2 ⤌ 2 ⤌ 2 ⤌ 4
2 ⤌ 2 ⤌ 8
2 ⤌ 4 ⤌ 4
2 ⤌ 16
4 ⤌ 8
*/
num = 11;
/*
num = 11
---------------
None
*/
}
}

#### Output

Given Number : 56
2 2 14
2 28

Given Number : 32
2 2 2 2 2
2 2 2 4
2 2 8
2 4 4
2 16
4 8

Given Number : 11
// Swift 4 Program
// Generate all the products of a number in which
// each element is divisible by the previous element
class Combinations
{
// Display result
func printSequence(_ result: [Int], _ n: Int)
{
var i: Int = 0;
while (i < n)
{
print(" ", result[i], terminator: "");
i += 1;
}
print(terminator: "\n");
}
func findCombination(_ num: Int,
_ result: inout[Int],
_ index: Int,
_ product: Int)
{
if (product == num)
{
// Display calculated result
self.printSequence(result, index);
return;
}
if (index >= num || product > num)
{
// Base case when stop process
return;
}
var i: Int = 2;
while (i <= num / 2)
{
if (index == 0 || i % result[index - 1] == 0)
{
// Collects resultant value
result[index] = i;
// Find other combination using recursion
self.findCombination(num, &result, index + 1, product * i);
}
i += 1;
}
}
func combination(_ num: Int)
{
if (num <= 0)
{
return;
}
if (num == 1)
{
print("\n 1 ");
return;
}
print("\n Given Number : ", num);
// Collect result
var result: [Int] = Array(repeating: 0, count: num);
// Test
self.findCombination(num, &result, 0, 1);
}
}
func main()
{
var num: Int = 56;
/*
num = 56
--------
2 ⤌ 2 ⤌ 14
2 ⤌ 28
---------------
Here  2  % 2  == 0   14 % 2 == 0
28 % 2 == 0
Element divisible previous element
*/
num = 32;
/*
num = 32
---------------
2 ⤌ 2 ⤌ 2 ⤌ 2 ⤌ 2
2 ⤌ 2 ⤌ 2 ⤌ 4
2 ⤌ 2 ⤌ 8
2 ⤌ 4 ⤌ 4
2 ⤌ 16
4 ⤌ 8
*/
num = 11;
/*
num = 11
---------------
None
*/
}
main();

#### Output

Given Number :  56
2  2  14
2  28

Given Number :  32
2  2  2  2  2
2  2  2  4
2  2  8
2  4  4
2  16
4  8

Given Number :  11
// Kotlin Program
// Generate all the products of a number in which
// each element is divisible by the previous element
class Combinations
{
// Display result
fun printSequence(result: Array < Int > , n: Int): Unit
{
var i: Int = 0;
while (i < n)
{
print(" " + result[i]);
i += 1;
}
print("\n");
}
fun findCombination(num: Int,
result: Array < Int > ,
index: Int, product: Int): Unit
{
if (product == num)
{
// Display calculated result
this.printSequence(result, index);
return;
}
if (index >= num || product > num)
{
// Base case when stop process
return;
}
var i: Int = 2;
while (i <= num / 2)
{
if (index == 0 || i % result[index - 1] == 0)
{
// Collects resultant value
result[index] = i;
// Find other combination using recursion
this.findCombination(num, result, index + 1, product * i);
}
i += 1;
}
}
fun combination(num: Int): Unit
{
if (num <= 0)
{
return;
}
if (num == 1)
{
print("\n 1 \n");
return;
}
println("\n Given Number : " + num);
// Collect result
val result: Array < Int > = Array(num)
{
0
};
// Test
this.findCombination(num, result, 0, 1);
}
}
fun main(args: Array < String > ): Unit
{
var num: Int = 56;
/*
num = 56
--------
2 ⤌ 2 ⤌ 14
2 ⤌ 28
---------------
Here  2  % 2  == 0   14 % 2 == 0
28 % 2 == 0
Element divisible previous element
*/
num = 32;
/*
num = 32
---------------
2 ⤌ 2 ⤌ 2 ⤌ 2 ⤌ 2
2 ⤌ 2 ⤌ 2 ⤌ 4
2 ⤌ 2 ⤌ 8
2 ⤌ 4 ⤌ 4
2 ⤌ 16
4 ⤌ 8
*/
num = 11;
/*
num = 11
---------------
None
*/
}

#### Output

Given Number : 56
2 2 14
2 28

Given Number : 32
2 2 2 2 2
2 2 2 4
2 2 8
2 4 4
2 16
4 8

Given Number : 11

## 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.