# C++ Program for Largest K digit number divisible by X

Integers X and K are given. The task is to find highest K-digit number divisible by X.

Examples:

Input : X = 30, K = 3 Output : 990 990 is the largest three digit number divisible by 30. Input : X = 7, K = 2 Output : 98

An **efficient solution **is to use below formula.

ans = MAX - (MAX % X) where MAX is the largest K digit number which is 999...K-times

The formula works on simple school method division. We remove remainder to get the largest divisible number.

`// CPP code to find highest K-digit number divisible by X ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to compute the result ` `int` `answer(` `int` `X, ` `int` `K) ` `{ ` ` ` `// Computing MAX ` ` ` `int` `MAX = ` `pow` `(10, K) - 1; ` ` ` ` ` `// returning ans ` ` ` `return` `(MAX - (MAX % X)); ` `} ` ` ` `// Driver ` `int` `main() ` `{ ` ` ` `// Number whose divisible is to be found ` ` ` `int` `X = 30; ` ` ` ` ` `// Max K-digit divisible is to be found ` ` ` `int` `K = 3; ` ` ` ` ` `cout << answer(X, K); ` `} ` |

*chevron_right*

*filter_none*

**Output:**

990

Please refer complete article on Largest K digit number divisible by X for more details!

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Java Program for Largest K digit number divisible by X
- Largest K digit number divisible by X
- Largest N digit number divisible by given three numbers
- C++ Program for Smallest K digit number divisible by X
- Java Program for Smallest K digit number divisible by X
- Largest number less than N with digit sum greater than the digit sum of N
- Find nth number that contains the digit k or divisible by k.
- Sum of n digit numbers divisible by a given number
- Smallest K digit number divisible by X
- Smallest N digit number divisible by N
- Find a N-digit number such that it is not divisible by any of its digits
- Find a N-digit number such that it is not divisible by any of its digits
- Count n digit numbers divisible by given number
- Smallest n digit number divisible by given three numbers
- Largest and smallest digit of a number
- Largest even digit number not greater than N
- Largest value of x such that ax
^{x}is N-digit number of base b - Largest number divisible by 90 that can be made using 0 and 5
- Largest number with the given set of N digits that is divisible by 2, 3 and 5
- Print digit's position to be removed to make a number divisible by 6