Skip to main content

Product of factors of number

Here given code implementation process.

// C Program
// Product of factors of number
#include <stdio.h>

// Calculate factors product of a number
void factorProduct(int num)
{
	printf("\n Factors of a number %d is \n", num);
	int result = num;
	// Execute loop through by 2 to num/2
	for (int i = 2; i <= num / 2; ++i)
	{
		if (num % i == 0)
		{
			printf(" %d", i);
			// calculated product 
			result *= i;
		}
	}
	// Last factors
	printf(" %d", num);
	// Display calculated result
	printf("\n Factors Product : %d\n", result);
}
int main()
{
	// Test Cases
	factorProduct(15);
	factorProduct(40);
	factorProduct(31);
	return 0;
}

Output

 Factors of a number 15 is
 3 5 15
 Factors Product : 225

 Factors of a number 40 is
 2 4 5 8 10 20 40
 Factors Product : 2560000

 Factors of a number 31 is
 31
 Factors Product : 31
/*
   Java Program
   Product of factors of number
*/
public class Factorization
{
	// Calculate factors product of a number
	public void factorProduct(int num)
	{
		System.out.print("\n Factors of a number " + num + " is \n");
		int result = num;
		// Execute loop through by 2 to num/2
		for (int i = 2; i <= num / 2; ++i)
		{
			if (num % i == 0)
			{
				System.out.print(" " + i);
				// calculated product 
				result *= i;
			}
		}
		// Last factors
		System.out.print(" " + num);
		// Display calculated result
		System.out.print("\n Factors Product : " + result + "\n");
	}
	public static void main(String[] args)
	{
		Factorization task = new Factorization();
		// Test Cases
		task.factorProduct(15);
		task.factorProduct(40);
		task.factorProduct(31);
	}
}

Output

 Factors of a number 15 is
 3 5 15
 Factors Product : 225

 Factors of a number 40 is
 2 4 5 8 10 20 40
 Factors Product : 2560000

 Factors of a number 31 is
 31
 Factors Product : 31
// Include header file
#include <iostream>
using namespace std;

/*
   C++ Program
   Product of factors of number
*/

class Factorization
{
	public:
		// Calculate factors product of a number
		void factorProduct(int num)
		{
			cout << "\n Factors of a number " << num << " is \n";
			int result = num;
			// Execute loop through by 2 to num/2
			for (int i = 2; i <= num / 2; ++i)
			{
				if (num % i == 0)
				{
					cout << " " << i;
					// calculated product
					result *= i;
				}
			}
			// Last factors
			cout << " " << num;
			// Display calculated result
			cout << "\n Factors Product : " << result << "\n";
		}
};
int main()
{
	Factorization task = Factorization();
	// Test Cases
	task.factorProduct(15);
	task.factorProduct(40);
	task.factorProduct(31);
	return 0;
}

Output

 Factors of a number 15 is
 3 5 15
 Factors Product : 225

 Factors of a number 40 is
 2 4 5 8 10 20 40
 Factors Product : 2560000

 Factors of a number 31 is
 31
 Factors Product : 31
// Include namespace system
using System;
/*
   C# Program
   Product of factors of number
*/
public class Factorization
{
	// Calculate factors product of a number
	public void factorProduct(int num)
	{
		Console.Write("\n Factors of a number " + num + " is \n");
		int result = num;
		// Execute loop through by 2 to num/2
		for (int i = 2; i <= num / 2; ++i)
		{
			if (num % i == 0)
			{
				Console.Write(" " + i);
				// calculated product
				result *= i;
			}
		}
		// Last factors
		Console.Write(" " + num);
		// Display calculated result
		Console.Write("\n Factors Product : " + result + "\n");
	}
	public static void Main(String[] args)
	{
		Factorization task = new Factorization();
		// Test Cases
		task.factorProduct(15);
		task.factorProduct(40);
		task.factorProduct(31);
	}
}

Output

 Factors of a number 15 is
 3 5 15
 Factors Product : 225

 Factors of a number 40 is
 2 4 5 8 10 20 40
 Factors Product : 2560000

 Factors of a number 31 is
 31
 Factors Product : 31
<?php
/*
   Php Program
   Product of factors of number
*/
class Factorization
{
	// Calculate factors product of a number
	public	function factorProduct($num)
	{
		echo "\n Factors of a number ". $num ." is \n";
		$result = $num;
		// Execute loop through by 2 to num/2
		for ($i = 2; $i <= intval($num / 2); ++$i)
		{
			if ($num % $i == 0)
			{
				echo " ". $i;
				// calculated product
				$result *= $i;
			}
		}
		// Last factors
		echo " ". $num;
		// Display calculated result
		echo "\n Factors Product : ". $result ."\n";
	}
}

function main()
{
	$task = new Factorization();
	// Test Cases
	$task->factorProduct(15);
	$task->factorProduct(40);
	$task->factorProduct(31);
}
main();

Output

 Factors of a number 15 is
 3 5 15
 Factors Product : 225

 Factors of a number 40 is
 2 4 5 8 10 20 40
 Factors Product : 2560000

 Factors of a number 31 is
 31
 Factors Product : 31
/*
   Node Js Program
   Product of factors of number
*/
class Factorization
{
	// Calculate factors product of a number
	factorProduct(num)
	{
		process.stdout.write("\n Factors of a number " + num + " is \n");
		var result = num;
		// Execute loop through by 2 to num/2
		for (var i = 2; i <= parseInt(num / 2); ++i)
		{
			if (num % i == 0)
			{
				process.stdout.write(" " + i);
				// calculated product
				result *= i;
			}
		}
		// Last factors
		process.stdout.write(" " + num);
		// Display calculated result
		process.stdout.write("\n Factors Product : " + result + "\n");
	}
}

function main()
{
	var task = new Factorization();
	// Test Cases
	task.factorProduct(15);
	task.factorProduct(40);
	task.factorProduct(31);
}
main();

Output

 Factors of a number 15 is
 3 5 15
 Factors Product : 225

 Factors of a number 40 is
 2 4 5 8 10 20 40
 Factors Product : 2560000

 Factors of a number 31 is
 31
 Factors Product : 31
#  Python 3 Program
#  Product of factors of number

class Factorization :
	#  Calculate factors product of a number
	def factorProduct(self, num) :
		print("\n Factors of a number ", num ," is ")
		result = num
		i = 2
		#  Execute loop through by 2 to num/2
		while (i <= int(num / 2)) :
			if (num % i == 0) :
				print(" ", i, end = "")
				#  calculated product
				result *= i
			
			i += 1
		
		#  Last factors
		print(" ", num, end = "")
		#  Display calculated result
		print("\n Factors Product :", result )
	

def main() :
	task = Factorization()
	#  Test Cases
	task.factorProduct(15)
	task.factorProduct(40)
	task.factorProduct(31)

if __name__ == "__main__": main()

Output

 Factors of a number  15  is
  3  5  15
 Factors Product : 225

 Factors of a number  40  is
  2  4  5  8  10  20  40
 Factors Product : 2560000

 Factors of a number  31  is
  31
 Factors Product : 31
# Ruby Program
# Product of factors of number

class Factorization 
	#  Calculate factors product of a number
	def factorProduct(num) 
		print("\n Factors of a number ", num ," is \n")
		result = num
		i = 2
		#  Execute loop through by 2 to num/2
		while (i <= num / 2) 
			if (num % i == 0) 
				print(" ", i)
				#  calculated product
				result *= i
			end

			i += 1
		end

		#  Last factors
		print(" ", num)
		#  Display calculated result
		print("\n Factors Product : ", result ,"\n")
	end

end

def main() 
	task = Factorization.new()
	#  Test Cases
	task.factorProduct(15)
	task.factorProduct(40)
	task.factorProduct(31)
end

main()

Output

 Factors of a number 15 is 
 3 5 15
 Factors Product : 225

 Factors of a number 40 is 
 2 4 5 8 10 20 40
 Factors Product : 2560000

 Factors of a number 31 is 
 31
 Factors Product : 31
/*
   Scala Program
   Product of factors of number
*/
class Factorization
{
	// Calculate factors product of a number
	def factorProduct(num: Int): Unit = {
		print("\n Factors of a number " + num + " is \n");
		var result: Int = num;
		var i: Int = 2;
		// Execute loop through by 2 to num/2
		while (i <= (num / 2).toInt)
		{
			if (num % i == 0)
			{
				print(" " + i);
				// calculated product
				result *= i;
			}
			i += 1;
		}
		// Last factors
		print(" " + num);
		// Display calculated result
		print("\n Factors Product : " + result + "\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Factorization = new Factorization();
		// Test Cases
		task.factorProduct(15);
		task.factorProduct(40);
		task.factorProduct(31);
	}
}

Output

 Factors of a number 15 is
 3 5 15
 Factors Product : 225

 Factors of a number 40 is
 2 4 5 8 10 20 40
 Factors Product : 2560000

 Factors of a number 31 is
 31
 Factors Product : 31
/*
   Swift 4 Program
   Product of factors of number
*/
class Factorization
{
	// Calculate factors product of a number
	func factorProduct(_ num: Int)
	{
		print("\n Factors of a number ", num ," is ");
		var result: Int = num;
		var i: Int = 2;
		// Execute loop through by 2 to num/2
		while (i <= num / 2)
		{
			if (num % i == 0)
			{
				print(" ", i, terminator: "");
				// calculated product
				result *= i;
			}
			i += 1;
		}
		// Last factors
		print(" ", num, terminator: "");
		// Display calculated result
		print("\n Factors Product : ", result );
	}
}
func main()
{
	let task: Factorization = Factorization();
	// Test Cases
	task.factorProduct(15);
	task.factorProduct(40);
	task.factorProduct(31);
}
main();

Output

 Factors of a number  15  is
  3  5  15
 Factors Product :  225

 Factors of a number  40  is
  2  4  5  8  10  20  40
 Factors Product :  2560000

 Factors of a number  31  is
  31
 Factors Product :  31
/*
   Kotlin Program
   Product of factors of number
*/
class Factorization
{
	// Calculate factors product of a number
	fun factorProduct(num: Int): Unit
	{
		print("\n Factors of a number " + num + " is \n");
		var result: Int = num;
		var i: Int = 2;
		// Execute loop through by 2 to num/2
		while (i <= num / 2)
		{
			if (num % i == 0)
			{
				print(" " + i);
				// calculated product
				result *= i;
			}
			i += 1;
		}
		// Last factors
		print(" " + num);
		// Display calculated result
		print("\n Factors Product : " + result + "\n");
	}
}
fun main(args: Array < String > ): Unit
{
	var task: Factorization = Factorization();
	// Test Cases
	task.factorProduct(15);
	task.factorProduct(40);
	task.factorProduct(31);
}

Output

 Factors of a number 15 is
 3 5 15
 Factors Product : 225

 Factors of a number 40 is
 2 4 5 8 10 20 40
 Factors Product : 2560000

 Factors of a number 31 is
 31
 Factors Product : 31




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