Append X digits to the end of N to make it divisible by M | C ++ program to implement the above approach ; Function to check if the value of N by appending X digits on right side of N is divisible by M or not ; Base Case ; If N is divisible by M ; Update res ; Iterate over the range [ 0 , 9 ] ; If N is divisible by M by appending X digits ; Driver Code ; Stores the number by appending X digits on the right side of N
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isDiv ( int N , int X , int M , int & res ) { if ( X == 0 ) { if ( N % M == 0 ) { res = N ; return true ; } return false ; } for ( int i = 0 ; i <= 9 ; i ++ ) { if ( isDiv ( N * 10 + i , X - 1 , M , res ) ) { return true ; } } } int main ( ) { int N = 4 , M = 50 , X = 2 ; int res = -1 ; isDiv ( N , X , M , res ) ; cout << res ; }