Check if N can be represented as sum of positive integers containing digit D at least once | C ++ program for the above approach ; Function to check if N contains digit D in it ; Iterate until N is positive ; Find the last digit ; If the last digit is the same as digit D ; Return false ; Function to check if the value of N can be represented as sum of integers having digit d in it ; Iterate until N is positive ; Check if N contains digit D or not ; Subtracting D from N ; Return false ; Driver Code
#include <iostream> NEW_LINE using namespace std ; bool findDigit ( int N , int D ) { while ( N > 0 ) { int a = N % 10 ; if ( a == D ) { return true ; } N /= 10 ; } return false ; } bool check ( int N , int D ) { while ( N > 0 ) { if ( findDigit ( N , D ) == true ) { return true ; } N -= D ; } return false ; } int main ( ) { int N = 24 ; int D = 7 ; if ( check ( N , D ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }