text: Find nth number that contains the digit k or divisible by k . | C ++ program to find nth number that contains the digit k or divisible by k . ; Function for checking if digit k is in n or not ; finding remainder ; if digit found ; Function for finding nth number ; since k is the first which satisfy the criteria , so consider it in count making count = 1 and starting from i = k + 1 ; checking that the number contain k digit or divisible by k ; Driver code
code: #include <bits/stdc++.h> NEW_LINE using namespace std ; int checkdigit ( int n , int k ) { while ( n ) { int rem = n % 10 ; if ( rem == k ) return 1 ; n = n / 10 ; } return 0 ; } int findNthNumber ( int n , int k ) { for ( int i = k + 1 , count = 1 ; count < n ; i ++ ) { if ( checkdigit ( i , k ) || ( i % k == 0 ) ) count ++ ; if ( count == n ) return i ; } return -1 ; } int main ( ) { int n = 10 , k = 2 ; cout << findNthNumber ( n , k ) << endl ; return 0 ; }