Count of numbers from the range [ L , R ] which contains at least one digit that divides K | Java implementation of the approach ; Function that returns true if num contains at least one digit that divides k ; Get the last digit ; If the digit is non - zero and it divides k ; Remove the last digit ; There is no digit in num that divides k ; Function to return the required count of elements from the given range which contain at least one digit that divides k ; To store the result ; For every number from the range ; If any digit of the current number divides k ; Driver code
class GFG { static boolean digitDividesK ( int num , int k ) { while ( num != 0 ) { int d = num % 10 ; if ( d != 0 && k % d == 0 ) return true ; num = num / 10 ; } return false ; } static int findCount ( int l , int r , int k ) { int count = 0 ; for ( int i = l ; i <= r ; i ++ ) { if ( digitDividesK ( i , k ) ) count ++ ; } return count ; } public static void main ( String [ ] args ) { int l = 20 , r = 35 ; int k = 45 ; System . out . println ( findCount ( l , r , k ) ) ; } }