Smallest number greater than or equal to N which is divisible by its non | C ++ program for the above approach ; Function to find the smallest number greater than or equal to N such that it is divisible by its non - zero digits ; Iterate in range [ N , N + 2520 ] ; To check if the current number satisfies the given condition ; Store the number in a temporary variable ; Loop until temp > 0 ; Check only for non zero digits ; Extract the current digit ; If number is divisible by current digit or not ; Otherwise , set possible to 0 ; Break out of the loop ; Divide by 10 ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findSmallestNumber ( int n ) { for ( int i = n ; i <= ( n + 2520 ) ; ++ i ) { bool possible = 1 ; int temp = i ; while ( temp ) { if ( temp % 10 != 0 ) { int digit = temp % 10 ; if ( i % digit != 0 ) { possible = 0 ; break ; } } temp /= 10 ; } if ( possible == 1 ) { cout << i ; return ; } } } int main ( ) { int N = 31 ; findSmallestNumber ( N ) ; return 0 ; }