
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Smallest K-Digit Number Divisible by X in C++
Smallest K digit number that divisible by X is found using the formula by checking divisible by X. The formula works in the following way −
Compute minimum K digit number [min] for example: 10/100/1000 etc.
Now find if min is divisible by X. if yes, then this is the answer.
If not, then min+X - ([min+X]%k) is the answer.
Example
#include <iostream> #include <math.h> using namespace std; int main() { int X = 83; int K = 5; cout<<"The smallest "<<K<<" digit number divisible by "<<X<<" is "; int MIN = pow(10, K - 1); if (MIN % X == 0) cout<<MIN; cout<<((MIN + X) - ((MIN + X) % X)); cout << answer(X, K); }
Output
The smallest 5 digit number divisible by 83 is 100430
Advertisements