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.
// 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 contributes by Mohit Gupta_OMG <(0_o)> |
chevron_right
filter_none
Output:
990
To understand Math.pow() function, please refer point 18 of the article :
https://www.geeksforgeeks.org/java-lang-math-class-java-set-2/
Please refer complete article on Largest K digit number divisible by X for more details!
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:
- C++ Program for Largest K digit number divisible by X
- Java Program for Smallest 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
- Largest number less than N with digit sum greater than the digit sum of N
- Java Program to check whether it is possible to make a divisible by 3 number using all digits in an array
- Java Program for Find largest prime factor of a number
- Sum of n digit numbers divisible by a given number
- Smallest N digit number divisible by N
- Smallest K digit number divisible by X
- Find nth number that contains the digit k or divisible by k.
- 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
- Find a N-digit number such that it is not divisible by any of its digits
- Largest even digit number not greater than N
- Largest value of x such that axx is N-digit number of base b
- Largest N digit number in Base B
- Largest and smallest digit of a number

