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

Input: N = 11, K = 3
Output: 12
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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++ implementation of the above approach ` `#include ` `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; ` `} `

 `// 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 ` `} `

 `# 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# 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 ` `} `

 ` `

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

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.

Article Tags :