- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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
C++ Program for the Largest K digit number divisible by X?
Two integers X and K are given. K is the number of digit in integer number. The logic is to find largest K-digit number divisible by X.
Input: X = 30, K = 3 Output: 980
Explanation
980 is the largest three digit number divisible by 30. By taking the K in power of 10 then subtracting it with 1 will give us the largest K digit number after that we will try to get the largest no. which is divided by X.
Example
#include <iostream> #include <math.h> using namespace std; int main() { int X = 20; int K = 3; int MAX = pow(10, K) - 1; cout << (MAX - (MAX % X)); }
Advertisements