- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 Articles
- 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
- Find N digits number which is divisible by D in C++
- Count pairs (i,j) such that (i+j) is divisible by both A and B in C++
- Print all the combinations of N elements by changing sign such that their sum is divisible by M in C++
- It is given that 65610 is divisible by 27. Which two numbers nearest to 65610 are each divisible by 27?
- Largest number with the given set of N digits that is divisible by 2, 3 and 5 in C++
- Find an array element such that all elements are divisible by it using c++
- Find a non empty subset in an array of N integers such that sum of elements of subset is divisible by N in C++
- N digit numbers divisible by 5 formed from the M digits in C++
- Generating a random number that is divisible by n in JavaScript
- If $overline{98125x2}$ is a number with $x$ as its tens digits such that it is divisible by 4. Find all the possible values of $x$.
- Check if N is divisible by a number which is composed of the digits from the set {A, B} in Python
- Find a number x such that sum of x and its digits is equal to given n in C++
- C/C++ Program to check whether it is possible to make a divisible by 3 number using all digits in an array?

Advertisements