Given three integers **A**, **B** and **N**, repeat the following process **N** times:

- Add a digit to
**A**such that after adding it,**A**is divisible by**B**. - Print the smallest value of
**A**possible after**N**iterations of above above operation. - Print
**-1**if the operation fails.

Note : We need to check divisibility after every digit addition.

**Examples:**

Input:A = 10, B = 11, N = 1

Output:-1

No matter what digit you add, 10X will never be divisible by 11.

Input:A = 5, B = 3, N = 3

Output:5100

**Approach:** Bruteforce for the first digit to be added from **0** to **9**, if none of the digits make **A** divisible by **B** then the answer is **-1**. Otherwise add the first digit that satisfies the condition and then add **0** after that **(n-1)** times because if **A** is divisible by **B** then **A*10, A*100, …** will also be divisible by **B**.

Below is the implementation of the above approach:

## C++

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

*chevron_right*

*filter_none*

## Java

` ` `//Java implementation of the approach ` ` ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `static` `int` `addNDigits(` `int` `a, ` `int` `b, ` `int` `n) ` `{ ` ` ` `int` `num = a; ` ` ` ` ` `// Try all digits from (0 to 9) ` ` ` `for` `(` `int` `i = ` `0` `; i <= ` `9` `; i++) { ` ` ` `int` `tmp = a * ` `10` `+ i; ` ` ` `if` `(tmp % b == ` `0` `) { ` ` ` `a = tmp; ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Fails in the first move itself ` ` ` `if` `(num == a) ` ` ` `return` `-` `1` `; ` ` ` ` ` `// Add (n-1) 0's ` ` ` `for` `(` `int` `j = ` `0` `; j < n - ` `1` `; j++) ` ` ` `a *= ` `10` `; ` ` ` ` ` `return` `a; ` `} ` ` ` `// Driver Program to test above function ` ` ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` `int` `a = ` `5` `, b = ` `3` `, n = ` `3` `; ` ` ` `System.out.print( addNDigits(a, b, n)); ` ` ` `} ` `} ` `// This code is contributed by anuj_67.. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` `def` `addNDigits(a, b, n) : ` ` ` ` ` `num ` `=` `a ` ` ` ` ` `# Try all digits from (0 to 9) ` ` ` `for` `i ` `in` `range` `(` `10` `) : ` ` ` `tmp ` `=` `a ` `*` `10` `+` `i ` ` ` ` ` `if` `(tmp ` `%` `b ` `=` `=` `0` `) : ` ` ` `a ` `=` `tmp ` ` ` `break` ` ` ` ` `# Fails in the first move itself ` ` ` `if` `(num ` `=` `=` `a) : ` ` ` `return` `-` `1` ` ` ` ` `# Add (n-1) 0's ` ` ` `for` `j ` `in` `range` `(n ` `-` `1` `) : ` ` ` `a ` `*` `=` `10` ` ` ` ` `return` `a ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `a ` `=` `5` ` ` `b ` `=` `3` ` ` `n ` `=` `3` ` ` ` ` `print` `(addNDigits(a, b, n)) ` ` ` `# This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `static` `int` `addNDigits(` `int` `a, ` ` ` `int` `b, ` `int` `n) ` `{ ` ` ` `int` `num = a; ` ` ` ` ` `// Try all digits from (0 to 9) ` ` ` `for` `(` `int` `i = 0; i <= 9; i++) ` ` ` `{ ` ` ` `int` `tmp = a * 10 + i; ` ` ` `if` `(tmp % b == 0) ` ` ` `{ ` ` ` `a = tmp; ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Fails in the first move itself ` ` ` `if` `(num == a) ` ` ` `return` `-1; ` ` ` ` ` `// Add (n-1) 0's ` ` ` `for` `(` `int` `j = 0; j < n - 1; j++) ` ` ` `a *= 10; ` ` ` ` ` `return` `a; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main () ` `{ ` ` ` `int` `a = 5, b = 3, n = 3; ` ` ` `Console.WriteLine(addNDigits(a, b, n)); ` `} ` `} ` ` ` `// This code is contributed ` `// by anuj_67.. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `function` `addNDigits(` `$a` `, ` `$b` `, ` `$n` `) ` `{ ` ` ` `$num` `= ` `$a` `; ` ` ` ` ` `// Try all digits from (0 to 9) ` ` ` `for` `(` `$i` `= 0; ` `$i` `<= 9; ` `$i` `++) ` ` ` `{ ` ` ` `$tmp` `= ` `$a` `* 10 + ` `$i` `; ` ` ` `if` `(` `$tmp` `% ` `$b` `== 0) ` ` ` `{ ` ` ` `$a` `= ` `$tmp` `; ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Fails in the first move itself ` ` ` `if` `(` `$num` `== ` `$a` `) ` ` ` `return` `-1; ` ` ` ` ` `// Add (n-1) 0's ` ` ` `for` `(` `$j` `= 0; ` `$j` `< ` `$n` `- 1; ` `$j` `++) ` ` ` `$a` `*= 10; ` ` ` ` ` `return` `$a` `; ` `} ` ` ` `// Driver Code ` `$a` `= 5; ` `$b` `= 3; ` `$n` `= 3; ` `echo` `addNDigits(` `$a` `, ` `$b` `, ` `$n` `); ` ` ` `// This code is contributed ` `// by Akanksha Rai ` |

*chevron_right*

*filter_none*

**Output:**

5100

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Number formed after K times repeated addition of smallest divisor of N
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Check if the sum of digits of number is divisible by all of its digits
- Modify array by merging elements with addition such that it consists of only Primes.
- Count the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i]
- Maximum splits in binary string such that each substring is divisible by given odd number
- Number of triplets such that each value is less than N and each pair sum is a multiple of K
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Minimum digits to be removed to make either all digits or alternating digits same
- Find a N-digit number such that it is not divisible by any of its digits
- Find a N-digit number such that it is not divisible by any of its digits
- Find N numbers such that a number and its reverse are divisible by sum of its digits
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Check perfect square using addition/subtraction
- Number Theory | Generators of finite cyclic group under addition
- Addition and Subtraction of Matrix using pthreads
- Addition of two numbers without propagating Carry
- Maximize occurrences of values between L and R on sequential addition of Array elements with modulo H

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.