Integers X and K are given. The task is to find the 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

A **simple solution** is to try all numbers starting from the largest K digit number (which is 999…K-times) and return the first number divisible by X.

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.

## C++

`// 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*

## Java

`// Java code to find highest ` `// K-digit number divisible by X ` ` ` `import` `java.io.*; ` `import` `java.lang.*; ` ` ` `class` `GFG { ` ` ` `public` `static` `double` `answer(` `double` `X, ` `double` `K) ` ` ` `{ ` ` ` `double` `i = ` `10` `; ` ` ` `// Computing MAX ` ` ` `double` `MAX = Math.pow(i, K) - ` `1` `; ` ` ` ` ` `// returning ans ` ` ` `return` `(MAX - (MAX % X)); ` ` ` `} ` ` ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` ` ` `// Number whose divisible is to be found ` ` ` `double` `X = ` `30` `; ` ` ` ` ` `// Max K-digit divisible is to be found ` ` ` `double` `K = ` `3` `; ` ` ` ` ` `System.out.println((` `int` `)answer(X, K)); ` ` ` `} ` `} ` ` ` `// Code contributed by Mohit Gupta_OMG <(0_o)> ` |

*chevron_right*

*filter_none*

## Python3

`# Python code to find highest ` `# K-digit number divisible by X ` ` ` `def` `answer(X, K): ` ` ` ` ` `# Computing MAX ` ` ` `MAX` `=` `pow` `(` `10` `, K) ` `-` `1` ` ` ` ` `# returning ans ` ` ` `return` `(` `MAX` `-` `(` `MAX` `%` `X)) ` ` ` `X ` `=` `30` `; ` `K ` `=` `3` `; ` ` ` `print` `(answer(X, K)); ` ` ` `# Code contributed by Mohit Gupta_OMG <(0_o)> ` |

*chevron_right*

*filter_none*

## C#

`// C# code to find highest ` `// K-digit number divisible by X ` ` ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `public` `static` `double` `answer(` `double` `X, ` `double` `K) ` ` ` `{ ` ` ` ` ` `double` `i = 10; ` ` ` ` ` `// Computing MAX ` ` ` `double` `MAX = Math.Pow(i, K) - 1; ` ` ` ` ` `// returning ans ` ` ` `return` `(MAX - (MAX % X)); ` ` ` `} ` ` ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` ` ` `// Number whose divisible is to be found ` ` ` `double` `X = 30; ` ` ` ` ` `// Max K-digit divisible is to be found ` ` ` `double` `K = 3; ` ` ` ` ` `Console.WriteLine((` `int` `)answer(X, K)); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP code to find highest K-digit ` `// number divisible by X ` ` ` `// Function to compute the result ` `function` `answer(` `$X` `, ` `$K` `) ` `{ ` ` ` ` ` `// Computing MAX ` ` ` `$MAX` `= pow(10, ` `$K` `) - 1; ` ` ` ` ` `// returning ans ` ` ` `return` `(` `$MAX` `- (` `$MAX` `% ` `$X` `)); ` `} ` ` ` `// Driver ` ` ` `// Number whose divisible ` ` ` `// is to be found ` ` ` `$X` `= 30; ` ` ` ` ` `// Max K-digit divisible ` ` ` `// is to be found ` ` ` `$K` `= 3; ` ` ` ` ` `echo` `answer(` `$X` `, ` `$K` `); ` ` ` `// This code is contributed by ajit ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

990

This article is contributed by **Rohit Thapliyal**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Largest number less than N with digit sum greater than the digit sum of N
- C++ Program for Largest K digit number divisible by X
- Java Program for Largest K digit number divisible by X
- Largest N digit number divisible by given three numbers
- Count of N-digit numbers having digit XOR as single digit
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Largest number less than N whose each digit is prime number
- Largest divisor of a number not divisible by another given number
- Find the remainder when First digit of a number is divided by its Last digit
- Last digit of a number raised to last digit of N factorial
- Min steps to convert N-digit prime number into another by replacing a digit in each step
- Count of Numbers in Range where first digit is equal to last digit of the number
- Smallest K digit number divisible by X
- C++ Program for Smallest K digit number divisible by X
- Java Program for Smallest K digit number divisible by X
- Print digit's position to be removed to make a number divisible by 6
- Smallest n digit number divisible by given three numbers
- Find nth number that contains the digit k or divisible by k.
- Count n digit numbers divisible by given number
- Sum of n digit numbers divisible by a given number