- Related Questions & Answers
- Find permutation of n which is divisible by 3 but not divisible by 6 in C++
- Count digits in given number N which divide N in C++
- Find the number closest to n and divisible by m in C++
- Find elements of an array which are divisible by N using STL in C++
- Find the Number which contain the digit d in C++
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M in C++
- Count n digit numbers divisible by given number in C++
- Generating a random number that is divisible by n in JavaScript
- C# Program to find whether the Number is Divisible by 2
- C Program to check if a number is divisible by sum of its digits
- C Program to check if a number is divisible by any of its digits
- How to find whether the Number is Divisible by 2 using C#?
- Add N digits to A such that it is divisible by B after each addition in C++?
- Find the first natural number whose factorial is divisible by x in C++
- Add N digits to A such that it is divisible by B after each addition?

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

Suppose we have two numbers N and D. We have to find N digit number, that is divisible by D. If N is 3, and D is 5, then the number can be 500. This can be solved easily. If D is 10 and N is 1, then it will be impossible. We can put D, and suppose the D has m number of digits, then attach N – m number of 0s to make it N digit number and divisible by D.

#include<iostream> using namespace std; string nDigitDivByD(int n, int d) { string ans = ""; if (d < 10) { ans += to_string(d); for (int i = 1; i < n; i++) ans += "0"; } else { if (n == 1) return "Cannot find any number"; else { string temp = to_string(d); ans += to_string(d); for (int i = 0; i < n-temp.length(); i++) ans += "0"; } } return ans; } int main() { int n = 5, d = 15; cout << nDigitDivByD(n, d); }

15000

Advertisements