Print all numbers up to N having product of digits equal to K | Java Program to implement the above approach ; Function to find the product of digits of a number ; Stores the product of digits of a number ; Return the product ; Function to print all numbers upto N having product of digits equal to K ; Stores whether any number satisfying the given conditions exists or not ; Iterate over the range [ 1 , N ] ; If product of digits of arr [ i ] is equal to K or not ; Print that number ; If no numbers are found ; Driver Code ; Given value of N & K ; Function call to print all numbers from [ 1 , N ] with product of digits K
import java . io . * ; import java . util . * ; class GFG { static int productOfDigits ( int N ) { int product = 1 ; while ( N != 0 ) { product = product * ( N % 10 ) ; N = N / 10 ; } return product ; } static void productOfDigitsK ( int N , int K ) { int flag = 0 ; for ( int i = 1 ; i <= N ; ++ i ) { if ( K == productOfDigits ( i ) ) { System . out . print ( i + " ▁ " ) ; flag = 1 ; } } if ( flag == 0 ) System . out . println ( - 1 ) ; } public static void main ( String [ ] args ) { int N = 500 , K = 10 ; productOfDigitsK ( N , K ) ; } }