- 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?
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
Explanation
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.
Example
#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; }
- Related Articles
- 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
- It is given that 65610 is divisible by 27. Which two numbers nearest to 65610 are each divisible by 27?
- Find N digits number which is divisible by D in C++
- 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$.
- Generating a random number that is divisible by n in JavaScript
- Check if N is divisible by a number which is composed of the digits from the set {A, B} in Python
- 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++
- Find an array element such that all elements are divisible by it using c++
- For any positive integer n, prove that $n^3-n$ is divisible by 6.
- Largest number with the given set of N digits that is divisible by 2, 3 and 5 in 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++
- Smallest number that is divisible by first n numbers in JavaScript
- If $n$ is an odd integer, then show that $n^2 - 1$ is divisible by $8$.
