- 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

# Find N digits number which is divisible by D in C++

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.

## Example

#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); }

## Output

15000

- Related Questions & Answers
- Sum which is divisible by n in JavaScript
- Find permutation of n which is divisible by 3 but not divisible by 6 in C++
- Check if N is divisible by a number which is composed of the digits from the set {A, B} in Python
- Count digits in given number N which divide N in C++
- Generating a random number that is divisible by n in JavaScript
- Smallest number that is divisible by first n numbers in JavaScript
- Find elements of an array which are divisible by N using STL in C++
- Find the number closest to n and divisible by m in C++
- Rearrangement of a number which is also divisible by it 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++
- Largest number with the given set of N digits that is divisible by 2, 3 and 5 in C++
- Add N digits to A such that it is divisible by B after each addition?
- Count n digit numbers divisible by given number in C++
- Greatest number divisible by n within a bound in JavaScript
- N digit numbers divisible by 5 formed from the M digits in C++

Advertisements