Find all numbers having digit product equal to k in 1 to N

Here given code implementation process.

/*
    Java program for
    Find all numbers having digit product equal to k in 1 to N
*/
public class Product
{
	public int findProduct(int num)
	{
		int n = num;
		int product = 1;
		// Calculate digit product
		while (n != 0 && product > 0)
		{
			product *= (n % 10);
			n = n / 10;
		}
		return product;
	}
	public void digitProduct(int n, int k)
	{
		if (n < 0)
		{
			return;
		}
		boolean result = false;
		System.out.print("\n Given k " + k);
		System.out.print("\n Product of number digit from (1 to " + 
                         n + ") is \n");
		for (int i = 0; i <= n; ++i)
		{
			if (findProduct(i) == k)
			{
				System.out.print("  " + i);
				result = true;
			}
		}
		if (result == false)
		{
			// When no result
			System.out.print(" None ");
		}
	}
	public static void main(String[] args)
	{
		Product task = new Product();
		// Test A
		int k = 40;
		int n = 100;
		/*
		    k = 40
		    n = 100
		    ----------
		    5 ✕ 8 = 40
		    8 ✕ 5 = 40
		    -----------
		    [58 84]
		*/
		task.digitProduct(n, k);
		// Test B
		k = 0;
		n = 80;
		/*
		    k = 0
		    n = 80
		    ------------
		    1 ✕ 0 = 0
		    2 ✕ 0 = 0
		    3 ✕ 0 = 0
		    4 ✕ 0 = 0
		    5 ✕ 0 = 0
		    6 ✕ 0 = 0 
		    7 ✕ 0 = 0
		    8 ✕ 0 = 0
		    ------------
		    [10  20  30  40  50  60  70  80]
		*/
		task.digitProduct(n, k);
	}
}

Output

 Given k 40
 Product of number digit from (1 to 100) is
  58  85
 Given k 0
 Product of number digit from (1 to 80) is
  10  20  30  40  50  60  70  80
// Include header file
#include <iostream>

using namespace std;
/*
    C++ program for
    Find all numbers having digit product equal to k in 1 to N
*/
class Product
{
	public: int findProduct(int num)
	{
		int n = num;
		int product = 1;
		// Calculate digit product
		while (n != 0 && product > 0)
		{
			product *= (n % 10);
			n = n / 10;
		}
		return product;
	}
	void digitProduct(int n, int k)
	{
		if (n < 0)
		{
			return;
		}
		bool result = false;
		cout << "\n Given k " << k;
		cout << "\n Product of number digit from (1 to " 
             << n << ") is \n";
		for (int i = 0; i <= n; ++i)
		{
			if (this->findProduct(i) == k)
			{
				cout << "  " << i;
				result = true;
			}
		}
		if (result == false)
		{
			// When no result
			cout << " None ";
		}
	}
};
int main()
{
	Product *task = new Product();
	// Test A
	int k = 40;
	int n = 100;
	/*
	    k = 40
	    n = 100
	    ----------
	    5 ✕ 8 = 40
	    8 ✕ 5 = 40
	    -----------
	    [58 84]
	*/
	task->digitProduct(n, k);
	// Test B
	k = 0;
	n = 80;
	/*
	    k = 0
	    n = 80
	    ------------
	    1 ✕ 0 = 0
	    2 ✕ 0 = 0
	    3 ✕ 0 = 0
	    4 ✕ 0 = 0
	    5 ✕ 0 = 0
	    6 ✕ 0 = 0 
	    7 ✕ 0 = 0
	    8 ✕ 0 = 0
	    ------------
	    [10  20  30  40  50  60  70  80]
	*/
	task->digitProduct(n, k);
	return 0;
}

Output

 Given k 40
 Product of number digit from (1 to 100) is
  58  85
 Given k 0
 Product of number digit from (1 to 80) is
  10  20  30  40  50  60  70  80
// Include namespace system
using System;
/*
    Csharp program for
    Find all numbers having digit product equal to k in 1 to N
*/
public class Product
{
	public int findProduct(int num)
	{
		int n = num;
		int product = 1;
		// Calculate digit product
		while (n != 0 && product > 0)
		{
			product *= (n % 10);
			n = n / 10;
		}
		return product;
	}
	public void digitProduct(int n, int k)
	{
		if (n < 0)
		{
			return;
		}
		Boolean result = false;
		Console.Write("\n Given k " + k);
		Console.Write("\n Product of number digit from (1 to " + 
                      n + ") is \n");
		for (int i = 0; i <= n; ++i)
		{
			if (this.findProduct(i) == k)
			{
				Console.Write("  " + i);
				result = true;
			}
		}
		if (result == false)
		{
			// When no result
			Console.Write(" None ");
		}
	}
	public static void Main(String[] args)
	{
		Product task = new Product();
		// Test A
		int k = 40;
		int n = 100;
		/*
		    k = 40
		    n = 100
		    ----------
		    5 ✕ 8 = 40
		    8 ✕ 5 = 40
		    -----------
		    [58 84]
		*/
		task.digitProduct(n, k);
		// Test B
		k = 0;
		n = 80;
		/*
		    k = 0
		    n = 80
		    ------------
		    1 ✕ 0 = 0
		    2 ✕ 0 = 0
		    3 ✕ 0 = 0
		    4 ✕ 0 = 0
		    5 ✕ 0 = 0
		    6 ✕ 0 = 0 
		    7 ✕ 0 = 0
		    8 ✕ 0 = 0
		    ------------
		    [10  20  30  40  50  60  70  80]
		*/
		task.digitProduct(n, k);
	}
}

Output

 Given k 40
 Product of number digit from (1 to 100) is
  58  85
 Given k 0
 Product of number digit from (1 to 80) is
  10  20  30  40  50  60  70  80
package main
import "fmt"
/*
    Go program for
    Find all numbers having digit product equal to k in 1 to N
*/

func findProduct(num int) int {
	var n int = num
	var product int = 1
	// Calculate digit product
	for (n != 0 && product > 0) {
		product *= (n % 10)
		n = n / 10
	}
	return product
}
func digitProduct(n, k int) {
	if n < 0 {
		return
	}
	var result bool = false
	fmt.Print("\n Given k ", k)
	fmt.Print("\n Product of number digit from (1 to ", 
		n, ") is \n")
	for i := 0 ; i <= n ; i++ {
		if findProduct(i) == k {
			fmt.Print("  ", i)
			result = true
		}
	}
	if result == false {
		// When no result
		fmt.Print(" None ")
	}
}
func main() {
	
	// Test A
	var k int = 40
	var n int = 100
	/*
	    k = 40
	    n = 100
	    ----------
	    5 ✕ 8 = 40
	    8 ✕ 5 = 40
	    -----------
	    [58 84]
	*/
	digitProduct(n, k)
	// Test B
	k = 0
	n = 80
	/*
	    k = 0
	    n = 80
	    ------------
	    1 ✕ 0 = 0
	    2 ✕ 0 = 0
	    3 ✕ 0 = 0
	    4 ✕ 0 = 0
	    5 ✕ 0 = 0
	    6 ✕ 0 = 0 
	    7 ✕ 0 = 0
	    8 ✕ 0 = 0
	    ------------
	    [10  20  30  40  50  60  70  80]
	*/
	digitProduct(n, k)
}

Output

 Given k 40
 Product of number digit from (1 to 100) is
  58  85
 Given k 0
 Product of number digit from (1 to 80) is
  10  20  30  40  50  60  70  80
<?php
/*
    Php program for
    Find all numbers having digit product equal to k in 1 to N
*/
class Product
{
	public	function findProduct($num)
	{
		$n = $num;
		$product = 1;
		// Calculate digit product
		while ($n != 0 && $product > 0)
		{
			$product *= ($n % 10);
			$n = (int)($n / 10);
		}
		return $product;
	}
	public	function digitProduct($n, $k)
	{
		if ($n < 0)
		{
			return;
		}
		$result = false;
		echo("\n Given k ".$k);
		echo("\n Product of number digit from (1 to ".$n.
			") is \n");
		for ($i = 0; $i <= $n; ++$i)
		{
			if ($this->findProduct($i) == $k)
			{
				echo("  ".$i);
				$result = true;
			}
		}
		if ($result == false)
		{
			// When no result
			echo(" None ");
		}
	}
}

function main()
{
	$task = new Product();
	// Test A
	$k = 40;
	$n = 100;
	/*
	    k = 40
	    n = 100
	    ----------
	    5 ✕ 8 = 40
	    8 ✕ 5 = 40
	    -----------
	    [58 84]
	*/
	$task->digitProduct($n, $k);
	// Test B
	$k = 0;
	$n = 80;
	/*
	    k = 0
	    n = 80
	    ------------
	    1 ✕ 0 = 0
	    2 ✕ 0 = 0
	    3 ✕ 0 = 0
	    4 ✕ 0 = 0
	    5 ✕ 0 = 0
	    6 ✕ 0 = 0 
	    7 ✕ 0 = 0
	    8 ✕ 0 = 0
	    ------------
	    [10  20  30  40  50  60  70  80]
	*/
	$task->digitProduct($n, $k);
}
main();

Output

 Given k 40
 Product of number digit from (1 to 100) is
  58  85
 Given k 0
 Product of number digit from (1 to 80) is
  10  20  30  40  50  60  70  80
/*
    Node JS program for
    Find all numbers having digit product equal to k in 1 to N
*/
class Product
{
	findProduct(num)
	{
		var n = num;
		var product = 1;
		// Calculate digit product
		while (n != 0 && product > 0)
		{
			product *= (n % 10);
			n = parseInt(n / 10);
		}
		return product;
	}
	digitProduct(n, k)
	{
		if (n < 0)
		{
			return;
		}
		var result = false;
		process.stdout.write("\n Given k " + k);
		process.stdout.write("\n Product of number digit from (1 to " + 
                             n + ") is \n");
		for (var i = 0; i <= n; ++i)
		{
			if (this.findProduct(i) == k)
			{
				process.stdout.write("  " + i);
				result = true;
			}
		}
		if (result == false)
		{
			// When no result
			process.stdout.write(" None ");
		}
	}
}

function main()
{
	var task = new Product();
	// Test A
	var k = 40;
	var n = 100;
	/*
	    k = 40
	    n = 100
	    ----------
	    5 ✕ 8 = 40
	    8 ✕ 5 = 40
	    -----------
	    [58 84]
	*/
	task.digitProduct(n, k);
	// Test B
	k = 0;
	n = 80;
	/*
	    k = 0
	    n = 80
	    ------------
	    1 ✕ 0 = 0
	    2 ✕ 0 = 0
	    3 ✕ 0 = 0
	    4 ✕ 0 = 0
	    5 ✕ 0 = 0
	    6 ✕ 0 = 0 
	    7 ✕ 0 = 0
	    8 ✕ 0 = 0
	    ------------
	    [10  20  30  40  50  60  70  80]
	*/
	task.digitProduct(n, k);
}
main();

Output

 Given k 40
 Product of number digit from (1 to 100) is
  58  85
 Given k 0
 Product of number digit from (1 to 80) is
  10  20  30  40  50  60  70  80
#    Python 3 program for
#    Find all numbers having digit product equal to k in 1 to N
class Product :
	def findProduct(self, num) :
		n = num
		product = 1
		#  Calculate digit product
		while (n != 0 and product > 0) :
			product *= (n % 10)
			n = int(n / 10)
		
		return product
	
	def digitProduct(self, n, k) :
		if (n < 0) :
			return
		
		result = False
		print("\n Given k ", k, end = "")
		print("\n Product of number digit from (1 to ",
              n ,") is ")
		i = 0
		while (i <= n) :
			if (self.findProduct(i) == k) :
				print("  ", i, end = "")
				result = True
			
			i += 1
		
		if (result == False) :
			#  When no result
			print(" None ", end = "")
		
	

def main() :
	task = Product()
	#  Test A
	k = 40
	n = 100
	#    k = 40
	#    n = 100
	#    ----------
	#    5 ✕ 8 = 40
	#    8 ✕ 5 = 40
	#    -----------
	#    [58 84]
	task.digitProduct(n, k)
	#  Test B
	k = 0
	n = 80
	#    k = 0
	#    n = 80
	#    ------------
	#    1 ✕ 0 = 0
	#    2 ✕ 0 = 0
	#    3 ✕ 0 = 0
	#    4 ✕ 0 = 0
	#    5 ✕ 0 = 0
	#    6 ✕ 0 = 0 
	#    7 ✕ 0 = 0
	#    8 ✕ 0 = 0
	#    ------------
	#    [10  20  30  40  50  60  70  80]
	task.digitProduct(n, k)

if __name__ == "__main__": main()

Output

 Given k  40
 Product of number digit from (1 to  100 ) is
   58   85
 Given k  0
 Product of number digit from (1 to  80 ) is
   10   20   30   40   50   60   70   80
#    Ruby program for
#    Find all numbers having digit product equal to k in 1 to N
class Product 
	def findProduct(num) 
		n = num
		product = 1
		#  Calculate digit product
		while (n != 0 && product > 0) 
			product *= (n % 10)
			n = n / 10
		end

		return product
	end

	def digitProduct(n, k) 
		if (n < 0) 
			return
		end

		result = false
		print("\n Given k ", k)
		print("\n Product of number digit from (1 to ", n ,") is \n")
		i = 0
		while (i <= n) 
			if (self.findProduct(i) == k) 
				print("  ", i)
				result = true
			end

			i += 1
		end

		if (result == false) 
			#  When no result
			print(" None ")
		end

	end

end

def main() 
	task = Product.new()
	#  Test A
	k = 40
	n = 100
	#    k = 40
	#    n = 100
	#    ----------
	#    5 ✕ 8 = 40
	#    8 ✕ 5 = 40
	#    -----------
	#    [58 84]
	task.digitProduct(n, k)
	#  Test B
	k = 0
	n = 80
	#    k = 0
	#    n = 80
	#    ------------
	#    1 ✕ 0 = 0
	#    2 ✕ 0 = 0
	#    3 ✕ 0 = 0
	#    4 ✕ 0 = 0
	#    5 ✕ 0 = 0
	#    6 ✕ 0 = 0 
	#    7 ✕ 0 = 0
	#    8 ✕ 0 = 0
	#    ------------
	#    [10  20  30  40  50  60  70  80]
	task.digitProduct(n, k)
end

main()

Output

 Given k 40
 Product of number digit from (1 to 100) is 
  58  85
 Given k 0
 Product of number digit from (1 to 80) is 
  10  20  30  40  50  60  70  80
/*
    Scala program for
    Find all numbers having digit product equal to k in 1 to N
*/
class Product()
{
	def findProduct(num: Int): Int = {
		var n: Int = num;
		var product: Int = 1;
		// Calculate digit product
		while (n != 0 && product > 0)
		{
			product *= (n % 10);
			n = n / 10;
		}
		return product;
	}
	def digitProduct(n: Int, k: Int): Unit = {
		if (n < 0)
		{
			return;
		}
		var result: Boolean = false;
		print("\n Given k " + k);
		print("\n Product of number digit from (1 to " + n + ") is \n");
		var i: Int = 0;
		while (i <= n)
		{
			if (findProduct(i) == k)
			{
				print("  " + i);
				result = true;
			}
			i += 1;
		}
		if (result == false)
		{
			// When no result
			print(" None ");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Product = new Product();
		// Test A
		var k: Int = 40;
		var n: Int = 100;
		/*
		    k = 40
		    n = 100
		    ----------
		    5 ✕ 8 = 40
		    8 ✕ 5 = 40
		    -----------
		    [58 84]
		*/
		task.digitProduct(n, k);
		// Test B
		k = 0;
		n = 80;
		/*
		    k = 0
		    n = 80
		    ------------
		    1 ✕ 0 = 0
		    2 ✕ 0 = 0
		    3 ✕ 0 = 0
		    4 ✕ 0 = 0
		    5 ✕ 0 = 0
		    6 ✕ 0 = 0 
		    7 ✕ 0 = 0
		    8 ✕ 0 = 0
		    ------------
		    [10  20  30  40  50  60  70  80]
		*/
		task.digitProduct(n, k);
	}
}

Output

 Given k 40
 Product of number digit from (1 to 100) is
  58  85
 Given k 0
 Product of number digit from (1 to 80) is
  10  20  30  40  50  60  70  80
/*
    Swift 4 program for
    Find all numbers having digit product equal to k in 1 to N
*/
class Product
{
	func findProduct(_ num: Int) -> Int
	{
		var n: Int = num;
		var product: Int = 1;
		// Calculate digit product
		while (n  != 0 && product > 0)
		{
			product *= (n % 10);
			n = n / 10;
		}
		return product;
	}
	func digitProduct(_ n: Int, _ k: Int)
	{
		if (n < 0)
		{
			return;
		}
		var result: Bool = false;
		print("\n Given k ", k, terminator: "");
		print("\n Product of number digit from (1 to ", n ,") is ");
		var i: Int = 0;
		while (i <= n)
		{
			if (self.findProduct(i) == k)
			{
				print("  ", i, terminator: "");
				result = true;
			}
			i += 1;
		}
		if (result == false)
		{
			// When no result
			print(" None ", terminator: "");
		}
	}
}
func main()
{
	let task: Product = Product();
	// Test A
	var k: Int = 40;
	var n: Int = 100;
	/*
	    k = 40
	    n = 100
	    ----------
	    5 ✕ 8 = 40
	    8 ✕ 5 = 40
	    -----------
	    [58 84]
	*/
	task.digitProduct(n, k);
	// Test B
	k = 0;
	n = 80;
	/*
	    k = 0
	    n = 80
	    ------------
	    1 ✕ 0 = 0
	    2 ✕ 0 = 0
	    3 ✕ 0 = 0
	    4 ✕ 0 = 0
	    5 ✕ 0 = 0
	    6 ✕ 0 = 0 
	    7 ✕ 0 = 0
	    8 ✕ 0 = 0
	    ------------
	    [10  20  30  40  50  60  70  80]
	*/
	task.digitProduct(n, k);
}
main();

Output

 Given k  40
 Product of number digit from (1 to  100 ) is
   58   85
 Given k  0
 Product of number digit from (1 to  80 ) is
   10   20   30   40   50   60   70   80
/*
    Kotlin program for
    Find all numbers having digit product equal to k in 1 to N
*/
class Product
{
	fun findProduct(num: Int): Int
	{
		var n: Int = num;
		var product: Int = 1;
		// Calculate digit product
		while (n != 0 && product > 0)
		{
			product *= (n % 10);
			n = n / 10;
		}
		return product;
	}
	fun digitProduct(n: Int, k: Int): Unit
	{
		if (n < 0)
		{
			return;
		}
		var result: Boolean = false;
		print("\n Given k " + k);
		print("\n Product of number digit from (1 to " + 
              n + ") is \n");
		var i: Int = 0;
		while (i <= n)
		{
			if (this.findProduct(i) == k)
			{
				print("  " + i);
				result = true;
			}
			i += 1;
		}
		if (result == false)
		{
			// When no result
			print(" None ");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Product = Product();
	// Test A
	var k: Int = 40;
	var n: Int = 100;
	/*
	    k = 40
	    n = 100
	    ----------
	    5 ✕ 8 = 40
	    8 ✕ 5 = 40
	    -----------
	    [58 84]
	*/
	task.digitProduct(n, k);
	// Test B
	k = 0;
	n = 80;
	/*
	    k = 0
	    n = 80
	    ------------
	    1 ✕ 0 = 0
	    2 ✕ 0 = 0
	    3 ✕ 0 = 0
	    4 ✕ 0 = 0
	    5 ✕ 0 = 0
	    6 ✕ 0 = 0 
	    7 ✕ 0 = 0
	    8 ✕ 0 = 0
	    ------------
	    [10  20  30  40  50  60  70  80]
	*/
	task.digitProduct(n, k);
}

Output

 Given k 40
 Product of number digit from (1 to 100) is
  58  85
 Given k 0
 Product of number digit from (1 to 80) is
  10  20  30  40  50  60  70  80


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