- Related Questions & Answers
- C Program to Check if all digits of a number divide it
- Sum of the numbers up to N that are divisible by 2 or 5 in c programming
- C Program for Print individual digits as words without using if or switch.
- Print first k digits of 1/n where n is a positive integer in C Program
- C++ Programming for Smallest K digit number divisible by X?
- All possible numbers of N digits and base B without leading zeros?
- C/C++ Program to check whether it is possible to make a divisible by 3 number using all digits in an array?
- Print values of ‘a’ in equation (a+b) <= n and a+b is divisible by x
- Print prime numbers with prime sum of digits in an array
- C Program for Difference between sums of odd and even digits?
- Addition of two numbers without propagating Carry?
- C++ Program for Largest K digit number divisible by X?
- Absolute difference between the first X and last X Digits of N?
- C++ Program for Smallest K digit number divisible by X?
- Add N digits to A such that it is divisible by B after each addition in C++?

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