- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

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

# Add N digits to A such that it is divisible by B after each addition in C++?

Here we will see how to generate a number A by adding N digits with it, and while adding new digits in each stage it will be divisible by another number B. Let us consider we are going to make a 5-digit number by adding 4 extra digits with it. We will check the divisibility by 7. The number will start from 8. So at first it will append 4 with it, so the number will be 84, that is divisible by 7. Then add 0 with the number so it will remain divisible by 7. If the number cannot be generated, it will return -1.

## Algorithm

### addNDigits(a, b, n)

begin num := a for all number x from 0 to 9, do temp := a * 10 + x if temp mod b is 0, then a := temp break end if done if num = a, then return -1 end if add remaining 0’s with a return a. end

## Example

#include<iostream> using namespace std; int add_n_digits(int a, int b, int n) { int num = a; for (int i = 0; i <= 9; i++) { //test by adding all digits (0-9) int tmp = a * 10 + i; if (tmp % b == 0) { a = tmp; //update a after adding break; } } if (num == a) //if no digit is added, return -1 return -1; for (int j = 0; j < n - 1; j++) //after getting divisible number, add 0s a *= 10; return a; } main() { int a, b, n; cout << "Enter A, B and N: "; cin >> a >> b >> n; int res = add_n_digits(a, b, n); if(res == -1) { cout << "Unable to get this type of number"; } else { cout << "Result is " << res; } }

## Output

Enter A, B and N: 8 7 4 Result is 84000

## Output

Enter A, B and N: 10 11 5 Unable to get this type of number

- Related Questions & Answers
- Add N digits to A such that it is divisible by B after each addition?
- 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++
- 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 all the combinations of N elements by changing sign such that their sum is divisible by M 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
- Print values of ‘a’ in equation (a+b) <= n and a+b is divisible by x
- Find a non empty subset in an array of N integers such that sum of elements of subset is divisible by N in C++
- Find a palindromic string B such that given String A is a subsequence of B in C++
- Largest number with the given set of N digits that is divisible by 2, 3 and 5 in C++
- Count of pairs from 1 to a and 1 to b whose sum is divisible by N in C++
- Smallest number that is divisible by first n numbers in JavaScript

Advertisements