Java Program for Largest K digit number divisible by X Last Updated : 05 Dec, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 // 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)> 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! Comment More infoAdvertise with us Next Article Java Program for Smallest K digit number divisible by X K kartik Follow Improve Article Tags : Mathematical Java Programs DSA Practice Tags : Mathematical Similar Reads Java Program for Smallest K digit number divisible by X Integers X and K are given. The task is to find the smallest K-digit number divisible by X. Examples: Input : X = 83, K = 5 Output : 10043 10040 is the smallest 5 digit number that is multiple of 83. Input : X = 5, K = 2 Output : 10 An efficient solution would be : Compute MIN : smallest K-digit num 2 min read Java Program to Minimize the Maximum Element of an Array Given two integers N and K. Create an array of N positive integers such that the sum of all elements of the array is divisible by K and the maximum element in the array is the minimum possible. You have to find the Maximum element of that array. Example: Input : N=5, K=11 Output : 3 Explanation : We 4 min read 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 i 1 min read C++ Program for Smallest K digit number divisible by X Integers X and K are given. The task is to find the smallest K-digit number divisible by X. Examples: Input : X = 83, K = 5 Output : 10043 10040 is the smallest 5 digit number that is multiple of 83. Input : X = 5, K = 2 Output : 10 An efficient solution would be : Compute MIN : smallest K-digit num 2 min read Kth largest N digit number divisible by M Given three positive integers N, K, and M. The task is to find Kth largest N digit number divisible by M. Note: K will be such an integer that Kth largest N digit number divisible by M always exists. Examples Input: N = 2, K = 2, M = 2Output: 96Explanation: The 2nd largest 2 digit number divisible b 5 min read Largest N digit number divisible by given three numbers 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. Examples: Input: x = 2, y = 3, z = 5, n = 4 Output: 9990 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 A 9 min read Like