Given four integers x, y, z and n, the task is to find the largest n digit number which is divisible by x, y and z.
Input: x = 2, y = 3, z = 5, n = 4
9990 is the largest 4-digit number which is divisible by 2, 3 and 5.
Input: x = 3, y = 23, z = 6, n = 2
Output: Not possible
- Find the largest n digit number i.e. pow(10, n) – 1 and store it in a variable largestN.
- Find LCM of the given three numbers x, y and z say LCM.
- Calculate the remainder when largestN is divided by LCM i.e. largestN % LCM and store it in a variable remainder.
- Subtract remainder from largestN. If the result is still an n digit number then print the result.
- Else print Not possible.
Below is the implementation of the above approach:
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Largest K digit number divisible by X
- C++ Program for Largest K digit number divisible by X
- Java Program for Largest K digit number divisible by X
- Sum of n digit numbers divisible by a given number
- Count n digit numbers divisible by given number
- Smallest n digit number divisible by given three numbers
- Largest Even and Odd N-digit numbers in Octal Number System
- Largest Even and Odd N-digit numbers in Hexadecimal Number System
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Largest number less than N with digit sum greater than the digit sum of N
- Largest Even and Odd N-digit numbers
- N digit numbers divisible by 5 formed from the M digits
- Sum of all N-digit palindromic numbers which doesn't contains 0 and are divisible by 9
- Count of N digit palindromic numbers divisible by 9
- Largest Even and Odd N-digit numbers of base B
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.