text: Count of numbers from the range [ L , R ] which contains at least one digit that divides K | C ++ 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
code: #include <bits/stdc++.h> NEW_LINE using namespace std ; bool digitDividesK ( int num , int k ) { while ( num ) { int d = num % 10 ; if ( d != 0 and k % d == 0 ) return true ; num = num / 10 ; } return false ; } 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 ; } int main ( ) { int l = 20 , r = 35 ; int k = 45 ; cout << findCount ( l , r , k ) ; return 0 ; }