Add N digits to A such that it is divisible by B after each addition | C ++ implementation of the approach ; Try all digits from ( 0 to 9 ) ; Fails in the first move itself ; Add ( n - 1 ) 0 's ; Driver Program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int addNDigits ( int a , int b , int n ) { int num = a ; for ( int i = 0 ; i <= 9 ; i ++ ) { int tmp = a * 10 + i ; if ( tmp % b == 0 ) { a = tmp ; break ; } } if ( num == a ) return -1 ; for ( int j = 0 ; j < n - 1 ; j ++ ) a *= 10 ; return a ; } int main ( ) { int a = 5 , b = 3 , n = 3 ; cout << addNDigits ( a , b , n ) ; return 0 ; }