- Related Questions & Answers
- Add N digits to A such that it is divisible by B after each addition in C++?
- Find the minimum positive integer such that it is divisible by A and sum of its digits is equal to B in Python
- Count number of pairs (A <= N, B <= N) such that gcd (A , B) is B in C++
- Count pairs (i,j) such that (i+j) is divisible by both A and B in C++
- Check if N is divisible by a number which is composed of the digits from the set {A, B} in Python
- Generating a random number that is divisible by n in JavaScript
- Find N digits number which is divisible by D in C++
- Find an array element such that all elements are divisible by it using c++
- Print values of ‘a’ in equation (a+b) <= n and a+b is divisible by x
- Print all the combinations of N elements by changing sign such that their sum is divisible by M in C++
- Smallest number that is divisible by first n numbers in JavaScript
- Find a non empty subset in an array of N integers such that sum of elements of subset is divisible by N in C++
- Largest number with the given set of N digits that is divisible by 2, 3 and 5 in C++
- Find a palindromic string B such that given String A is a subsequence of B in C++
- Count of pairs from 1 to a and 1 to b whose sum is divisible by N in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Given a, b and n. And we have to consider the following conditions and find the optimal solution to add n digits to a such that it is divisible by b after every iteration.

Add a digit to

**a**in such**a**way that after adding it,**a**is divisible by**b**.Print the smallest value of

**a**possible after n iterations of step1.Print

**fail**if the operation fails.

check divisibility after every digit addition.

**Input**

a=5 b=4 n=4

**Output**

52000

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** which means the if **n **digits are added in **a**. a never be divided by **b **. else 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**.

#include <iostream> using namespace std; int main() { int a = 5, b = 4, n = 4; int num = a; for (int i = 0; i <= 9; i++) { int temp = a * 10 + i; if (temp % b == 0) { a = temp; break; } } if (num == a) { a = -1; } for (int j = 0; j < n - 1; j++) { a *= 10; } if(a>-1) { cout <<a; } else { cout <<”fail”; } return 0; }

Advertisements