Check if N can be represented as sum of positive integers containing digit D at least once | Java approach 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
import java . util . * ; class GFG { static boolean findDigit ( int N , int D ) { while ( N > 0 ) { int a = N % 10 ; if ( a == D ) { return true ; } N /= 10 ; } return false ; } static boolean check ( int N , int D ) { while ( N > 0 ) { if ( findDigit ( N , D ) == true ) { return true ; } N -= D ; } return false ; } public static void main ( String [ ] args ) { int N = 24 ; int D = 7 ; if ( check ( N , D ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }