Largest number smaller than or equal to N divisible by K Last Updated : 25 Jul, 2022 Comments Improve Suggest changes Like Article Like Report Given a number N and a number K, the task is to find the largest number smaller than or equal to N which is divisible by K. Examples: Input: N = 45, K = 6 Output: 42 42 is the largest number smaller than or equal to 45 which is divisible by 6.Input: N = 11, K = 3 Output: 9 Approach: The idea is to divide the N by K. If the remainder is 0 then print N else print N - remainder. Below is the implementation of the above approach: C++ // C++ implementation of the above approach #include <bits/stdc++.h> using namespace std; // Function to find the largest number // smaller than or equal to N // that is divisible by k int findNum(int N, int K) { int rem = N % K; if (rem == 0) return N; else return N - rem; } // Driver code int main() { int N = 45, K = 6; cout << "Largest number smaller than or equal to "<< N << "\nthat is divisible by "<< K << " is " << findNum(N, K); return 0; } Java // Java implementation of the // above approach import java.lang.*; import java.util.*; class GFG { // Function to find the largest number // smaller than or equal to N // that is divisible by k static int findNum(int N, int K) { int rem = N % K; if (rem == 0) return N; else return N - rem; } // Driver code public static void main(String args[]) { int N = 45, K = 6; System.out.print("Largest number smaller " + "than or equal to " + N + "\nthat is divisible by " + K + " is " + findNum(N, K)); } } // This code is contributed // by Akanksha Rai(Abby_akku) Python3 # Python3 implementation of the above approach # Function to find the largest number smaller # than or equal to N that is divisible by k def findNum(N, K): rem = N % K if(rem == 0): return N else: return N - rem # Driver code if __name__=='__main__': N = 45 K = 6 print("Largest number smaller than or equal to" + str(N) + "that is divisible by" + str(K) + "is", findNum(N, K)) # This code is contributed by # Kirti_Mangal C# // C# implementation of the above approach using System; class GFG { // Function to find the largest number // smaller than or equal to N // that is divisible by k static int findNum(int N, int K) { int rem = N % K; if (rem == 0) return N; else return N - rem; } // Driver code public static void Main() { int N = 45, K = 6; Console.Write("Largest number smaller " + "than or equal to "+ N + "\nthat is divisible by "+ K + " is " + findNum(N, K)); } } // This code is contributed // by Akanksha Rai(Abby_akku) PHP <?php // PHP implementation of the // above approach // Function to find the largest // number smaller than or equal // to N that is divisible by k function findNum($N, $K) { $rem = $N % $K; if ($rem == 0) return $N; else return $N - $rem; } // Driver code $N = 45 ; $K = 6 ; echo"Largest number smaller than or equal to ", $N, "\nthat is divisible by ", $K, " is ", findNum($N, $K); // This code is contributed by ANKITRAI1 ?> JavaScript <script> // Javascript implementation of the above approach // Function to find the largest number // smaller than or equal to N // that is divisible by k function findNum(N, K) { var rem = N % K; if (rem == 0) return N; else return N - rem; } // Driver code var N = 45, K = 6; document.write( "Largest number smaller than or equal to " + N + "<br>that is divisible by " + K + " is " + findNum(N, K)); </script> Output: Largest number smaller than or equal to 45 that is divisible by 6 is 42 Time Complexity: O(1), since there is no loop or recursion. Auxiliary Space: O(1), since no extra space has been taken. Comment More infoAdvertise with us Next Article Smallest number divisible by n and has at-least k trailing zeros S Shivam.Pradhan Follow Improve Article Tags : Misc Mathematical DSA divisibility Number Divisibility +1 More Practice Tags : MathematicalMisc Similar Reads Smallest number greater than or equal to N divisible by K Given a number N and a number K, the task is to find the smallest number greater than or equal to N which is divisible by K.Examples: Input: N = 45, K = 6Output: 4848 is the smallest number greater than or equal to 45which is divisible by 6.Input: N = 11, K = 3Output: 12 Approach: Approach to solve 7 min read Smallest number divisible by n and has at-least k trailing zeros Two integers n and k are given. Our task is to print K-rounding of n. K-rounding is the minimum positive integer X, such that x ends with k or more zeros and is divisible by n.Examples : Input : n = 30, k = 3. Output : 3000 3000 is the smallest number that has at-least k 0s and is divisible by n. In 5 min read Smallest number divisible by first n numbers Given a number n find the smallest number evenly divisible by each number 1 to n.Examples: Input : n = 4 Output : 12 Explanation : 12 is the smallest numbers divisible by all numbers from 1 to 4 Input : n = 10 Output : 2520 Input : n = 20 Output : 232792560If you observe carefully the ans must be th 8 min read Find Nth smallest number that is divisible by 100 exactly K times Given two numbers N and K . The task is to find N'th smallest number that is divided by 100 exactly K times.Examples: Input : N = 12, K = 2 Output : 120000 120000 is divisible by 100 exactly 2 times and is the 12 th smallest number also.Input : N = 1000, K = 2 Output : 10010000 Approach: First, find 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 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 Like