Print all numbers up to N having product of digits equal to K - GeeksforGeeks

18 Nov, 2021
Given two integers N and K, the task is to print all the numbers from the range [1, N] whose product of digits is equal to K. If no such number is found, then print “-1”.
Examples:
Input: N =100, K = 25Output: 55Explanation: There is only a single number 55 whose product of digits is equal to K.
Input: N = 500, K = 10Output: 25 52 125 152 215 251 
Approach: Follow the steps below to solve the problem:
Initialize a variable, say flag, to store whether any number satisfying the given conditions exists or not.
Declare a function, productOfDigits(), to find the product of digits of the number.
Iterate over the range [1, N]:If the product of digits of arr[i] is equal to K, print that number and set flag = 1.
If the product of digits of arr[i] is equal to K, print that number and set flag = 1.
If flag is equal to 0, which means that no such number is found in the range [1, N]. Therefore, print “-1”.
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ program for the above approach #include <bits/stdc++.h>using namespace std; // Function to find the product// of digits of a numberint productOfDigits(int N){    // Stores the product of    // digits of a number    int product = 1;     while (N != 0) {        product = product * (N % 10);        N = N / 10;    }     // Return the product    return product;} // Function to print all numbers upto// N having product of digits equal to Kvoid productOfDigitsK(int N, int K){    // Stores whether any number satisfying    // the given conditions exists or not    int flag = 0;     // Iterate over the range [1, N]    for (int i = 1; i <= N; ++i) {         // If product of digits of        // arr[i] is equal to K or not        if (K == productOfDigits(i)) {             // Print that number            cout << i << " ";            flag = 1;        }    }     // If no numbers are found    if (flag == 0)        cout << "-1";} // Driver Codeint main(){    // Given value of N & K    int N = 500, K = 10;     // Function call to print all numbers    // from [1, N] with product of digits K    productOfDigitsK(N, K);}
// Java Program to implement// the above approachimport java.io.*;import java.util.*; class GFG {   // Function to find the product  // of digits of a number  static int productOfDigits(int N)  {         // Stores the product of    // digits of a number    int product = 1;     while (N != 0) {      product = product * (N % 10);      N = N / 10;    }     // Return the product    return product;  }   // Function to print all numbers upto  // N having product of digits equal to K  static void productOfDigitsK(int N, int K)  {    // Stores whether any number satisfying    // the given conditions exists or not    int flag = 0;     // Iterate over the range [1, N]    for (int i = 1; i <= N; ++i) {       // If product of digits of      // arr[i] is equal to K or not      if (K == productOfDigits(i)) {         // Print that number        System.out.print(i + " ");        flag = 1;      }    }     // If no numbers are found    if (flag == 0)      System.out.println(-1);  }   // Driver Code  public static void main(String[] args)  {         // Given value of N & K    int N = 500, K = 10;     // Function call to print all numbers    // from [1, N] with product of digits K    productOfDigitsK(N, K);  }} // This code is contribute by Kingash.
# Python3 program for the above approach # Function to find the product# of digits of a numberdef productOfDigits(N) :     # Stores the product of    # digits of a number    product = 1;     while (N != 0) :        product = product * (N % 10);        N = N // 10;     # Return the product    return product; # Function to print all numbers upto# N having product of digits equal to Kdef productOfDigitsK(N, K) :     # Stores whether any number satisfying    # the given conditions exists or not    flag = 0;     # Iterate over the range [1, N]    for i in range(1, N + 1) :         # If product of digits of        # arr[i] is equal to K or not        if (K == productOfDigits(i)) :             # Print that number            print(i, end =" ");            flag = 1;     # If no numbers are found    if (flag == 0) :        print("-1"); # Driver Codeif __name__ == "__main__" :         # Given value of N & K    N = 500; K = 10;     # Function call to print all numbers    # from [1, N] with product of digits K    productOfDigitsK(N, K);         # This code is contributed by AnkThon
// C# program for the above approachusing System;class GFG{   // Function to find the product  // of digits of a number  static int productOfDigits(int N)  {     // Stores the product of    // digits of a number    int product = 1;     while (N != 0) {      product = product * (N % 10);      N = N / 10;    }     // Return the product    return product;  }   // Function to print all numbers upto  // N having product of digits equal to K  static void productOfDigitsK(int N, int K)  {     // Stores whether any number satisfying    // the given conditions exists or not    int flag = 0;     // Iterate over the range [1, N]    for (int i = 1; i <= N; ++i) {       // If product of digits of      // arr[i] is equal to K or not      if (K == productOfDigits(i)) {         // Print that number        Console.Write(i + " ");        flag = 1;      }    }     // If no numbers are found    if (flag == 0)      Console.WriteLine(-1);  }    // Driver Code  static public void Main()  {     // Given value of N & K    int N = 500, K = 10;     // Function call to print all numbers    // from [1, N] with product of digits K    productOfDigitsK(N, K);  }} // This code is contributed by jana_sayantan.
<script> // Javascript program for the above approach   // Function to find the product  // of digits of a number  function productOfDigits(N)  {          // Stores the product of    // digits of a number    let product = 1;      while (N != 0) {      product = product * (N % 10);      N = Math.floor(N / 10);    }      // Return the product    return product;  }    // Function to print all numbers upto  // N having product of digits equal to K  function productOfDigitsK(N, K)  {    // Stores whether any number satisfying    // the given conditions exists or not    let flag = 0;      // Iterate over the range [1, N]    for (let i = 1; i <= N; ++i) {        // If product of digits of      // arr[i] is equal to K or not      if (K == productOfDigits(i)) {          // Print that number        document.write(i + " ");        flag = 1;      }    }      // If no numbers are found    if (flag == 0)      document.write(-1);  } // Driver Code      // Given value of N & K    let N = 500, K = 10;      // Function call to print all numbers    // from [1, N] with product of digits K    productOfDigitsK(N, K); </script>
25 52 125 152 215 251 
Time Complexity: O(N * logN)Auxiliary Space: O(1)
Kingash
jana_sayantan
ankthon
chinmoy1997pal
Kirti_Mangal
number-digits
Technical Scripter 2020
Mathematical
Searching
Technical Scripter
Searching
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Program to find GCD or HCF of two numbers
Sieve of Eratosthenes
Binary Search
Maximum and minimum of an array using minimum number of comparisons
Linear Search
Search an element in a sorted and rotated array
Find the Missing Number