Queries to count numbers from given range which are divisible by all its digits | C ++ program to implement the above approach ; Function to check if a number is divisible by all of its non - zero digits or not ; Stores the number ; Iterate over the digits of the numbers ; If digit of number is non - zero ; If number is not divisible by its current digit ; Update n ; Function to count of numbers which are divisible by all of its non - zero digits in the range [ 1 , i ] ; Stores count of numbers which are divisible by all of its non - zero digits in the range [ 1 , i ] ; Iterate over the range [ 1 , Max ] ; Update ; Traverse the array , arr [ ] ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define Max  1000005 NEW_LINE bool CheckDivByAllDigits ( int number ) { int n = number ; while ( n > 0 ) { if ( n % 10 ) if ( number % ( n % 10 ) ) { return false ; } n /= 10 ; } return true ; } void cntNumInRang ( int arr [ ] [ 2 ] , int N ) { int prefCntDiv [ Max ] = { 0 } ; for ( int i = 1 ; i <= Max ; i ++ ) { prefCntDiv [ i ] = prefCntDiv [ i - 1 ] + ( CheckDivByAllDigits ( i ) ) ; } for ( int i = 0 ; i < N ; i ++ ) cout << ( prefCntDiv [ arr [ i ] [ 1 ] ] - prefCntDiv [ arr [ i ] [ 0 ] - 1 ] ) << " ▁ " ; } int main ( ) { int arr [ ] [ 2 ] = { { 1 , 5 } , { 12 , 14 } } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cntNumInRang ( arr , N ) ; return 0 ; }