# 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:48 48 is the smallest number greater than or equal to 45 which is divisible by 6.Input:N = 11, K = 3Output:12

**Approach:** The idea is to divide the N+K by K. If the remainder is 0 then print **N** else print **N + K – 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 smallest number ` `// greater than or equal to N ` `// that is divisible by k ` `int` `findNum(` `int` `N, ` `int` `K) ` `{ ` ` ` `int` `rem = (N + K) % K; ` ` ` ` ` `if` `(rem == 0) ` ` ` `return` `N; ` ` ` `else` ` ` `return` `N + K - rem; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `N = 45, K = 6; ` ` ` ` ` `cout << ` `"Smallest number greater than or equal to "` `<< N ` ` ` `<< ` `"\nthat is divisible by "` `<< K << ` `" is "` `<< findNum(N, K); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the above approach ` ` ` `public` `class` `GFG{ ` ` ` ` ` `// Function to find the smallest number ` ` ` `// greater than or equal to N ` ` ` `// that is divisible by k ` ` ` `static` `int` `findNum(` `int` `N, ` `int` `K) ` ` ` `{ ` ` ` `int` `rem = (N + K) % K; ` ` ` ` ` `if` `(rem == ` `0` `) ` ` ` `return` `N; ` ` ` `else` ` ` `return` `N + K - rem; ` ` ` `} ` ` ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String []args){ ` ` ` ` ` `int` `N = ` `45` `, K = ` `6` `; ` ` ` ` ` `System.out.println(` `"Smallest number greater than or equal to "` `+ N ` ` ` `+` `"\nthat is divisible by "` `+ K + ` `" is "` `+ findNum(N, K)); ` ` ` ` ` `} ` ` ` `// This code is contributed by ANKITRAI1 ` `} ` |

*chevron_right*

*filter_none*

## Python

# Python 3 implementation of the

# above approach

# Function to find the smallest number

# greater than or equal to N

# that is divisible by k

def findNum(N, K):

rem = (N + K) % K;

if (rem == 0):

return N

else:

return (N + K – rem)

# Driver Code

N = 45

K = 6

print(‘Smallest number greater than’,

‘or equal to’ , N,

‘that is divisible by’, K,

‘is’ , findNum(45, 6))

# This code is contributed by Arnab Kundu

## C#

`// C# implementation of the above approach ` ` ` `public` `class` `GFG{ ` ` ` ` ` `// Function to find the smallest number ` ` ` `// greater than or equal to N ` ` ` `// that is divisible by k ` ` ` `static` `int` `findNum(` `int` `N, ` `int` `K) ` ` ` `{ ` ` ` `int` `rem = (N + K) % K; ` ` ` ` ` `if` `(rem == 0) ` ` ` `return` `N; ` ` ` `else` ` ` `return` `N + K - rem; ` ` ` `} ` ` ` ` ` ` ` `// Driver Code ` ` ` `static` `void` `Main(){ ` ` ` ` ` `int` `N = 45, K = 6; ` ` ` ` ` `System.Console.WriteLine(` `"Smallest number greater than or equal to "` `+ N ` ` ` `+` `"\nthat is divisible by "` `+ K + ` `" is "` `+ findNum(N, K)); ` ` ` ` ` `} ` ` ` `// This code is contributed by mits ` `} ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the above approach ` ` ` `// Function to find the smallest number ` `// greater than or equal to N that is ` `// divisible by k ` `function` `findNum(` `$N` `, ` `$K` `) ` `{ ` ` ` `$rem` `= (` `$N` `+ ` `$K` `) % ` `$K` `; ` ` ` ` ` `if` `(` `$rem` `== 0) ` ` ` `return` `$N` `; ` ` ` `else` ` ` `return` `$N` `+ ` `$K` `- ` `$rem` `; ` `} ` ` ` `// Driver code ` `$N` `= 45; ` `$K` `= 6; ` ` ` `echo` `"Smallest number greater than "` `. ` ` ` `"or equal to "` `, ` `$N` `; ` `echo` `"\nthat is divisible by "` `, ` `$K` `, ` ` ` `" is "` `, findNum(` `$N` `, ` `$K` `); ` ` ` `// This code is contributed by anuj_67 ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

Smallest number greater than or equal to 45 that is divisible by 6 is 48

## Recommended Posts:

- Smallest Special Prime which is greater than or equal to a given number
- Check whether all the rotations of a given number is greater than or equal to the given number or not
- Smallest number with sum of digits as N and divisible by 10^N
- Smallest number divisible by first n numbers
- Smallest K digit number divisible by X
- Smallest n digit number divisible by given three numbers
- Length of the smallest number which is divisible by K and formed by using 1's only
- Smallest number divisible by n and has at-least k trailing zeros
- C++ Program for Smallest K digit number divisible by X
- Java Program for Smallest K digit number divisible by X
- Largest number smaller than or equal to N divisible by K
- Smallest number k such that the product of digits of k is equal to n
- Smallest divisor D of N such that gcd(D, M) is greater than 1
- Smallest integer greater than n such that it consists of digit m exactly k times
- Smallest subarray with product divisible by k

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.