Count numbers from a given range whose product of digits is K | Java program to implement the above approach ; Function to find the product of digits of a number ; Stores product of digits of N ; Update res ; Update N ; Function to count numbers in the range [ 0 , X ] whose product of digit is K ; Stores count of numbers in the range [ L , R ] whose product of digit is K ; Iterate over the range [ L , R ] ; If product of digits of i equal to K ; Update cnt ; Driver Code
import java . util . * ; class GFG { static int prodOfDigit ( int N ) { int res = 1 ; while ( N > 0 ) { res = res * ( N % 10 ) ; N /= 10 ; } return res ; } static int cntNumRange ( int L , int R , int K ) { int cnt = 0 ; for ( int i = L ; i <= R ; i ++ ) { if ( prodOfDigit ( i ) == K ) { cnt ++ ; } } return cnt ; } public static void main ( String [ ] args ) { int L = 20 , R = 10000 , K = 14 ; System . out . print ( cntNumRange ( L , R , K ) ) ; } }