Minimum positive integer divisible by C and is not in range [A, B]

Given three positive integers A, B and C. The task is to find the minimum integer X > 0 such that:

1. X % C = 0 and
2. X must not belong to the range [A, B]

Examples:

Input: A = 2, B = 4, C = 2
Output: 6

Input: A = 5, B = 10, C = 4
Output: 4

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

Approach:

• If C doesn’t belong to [A, B] i.e. C < A or C > B then C is the required number.
• Else get the first multiple of C greater than B which is the required answer.

Below is the implementation of the above approach:

C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return the required number ` `int` `getMinNum(``int` `a, ``int` `b, ``int` `c) ` `{ ` ` `  `    ``// If doesn't belong to the range ` `    ``// then c is the required number ` `    ``if` `(c < a || c > b) ` `        ``return` `c; ` ` `  `    ``// Else get the next multiple of c ` `    ``// starting from b + 1 ` `    ``int` `x = ((b / c) * c) + c; ` ` `  `    ``return` `x; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `a = 2, b = 4, c = 4; ` `    ``cout << getMinNum(a, b, c); ` `    ``return` `0; ` `} `

Java

 `// Java implementation of the approach ` `import` `java.io.*;  ` `import` `java.math.*;  ` `public` `class` `GFG  ` `{  ` `    ``// Function to return the required number ` `    ``int` `getMinNum(``int` `a, ``int` `b, ``int` `c) ` `    ``{ ` ` `  `        ``// If doesn't belong to the range ` `        ``// then c is the required number ` `        ``if` `(c < a || c > b) ` `        ``{ ` `            ``return` `c; ` `        ``} ` ` `  `        ``// Else get the next multiple of c ` `        ``// starting from b + 1 ` `        ``int` `x = ((b / c) * c) + c; ` ` `  `        ``return` `x; ` `    ``} ` ` `  `// Driver code ` `public` `static` `void` `main(String args[]) ` `{  ` `    ``int` `a = ``2``; ` `    ``int` `b = ``4``; ` `    ``int` `c = ``4``; ` `    ``GFG g = ``new` `GFG(); ` `    ``System.out.println(g.getMinNum(a, b, c));  ` `}  ` `} ` ` `  `// This code is contributed by Shivi_Aggarwal `

Python3

 `# Python3 implementation of the approach ` `# Function to return the required number ` `def` `getMinNum(a, b, c): ` ` `  `    ``# If doesn't belong to the range ` `    ``# then c is the required number ` `    ``if` `(c < a ``or` `c > b): ` `        ``return` `c ` ` `  `    ``# Else get the next multiple of c ` `    ``# starting from b + 1 ` `    ``x ``=` `((b ``/``/` `c) ``*` `c) ``+` `c ` ` `  `    ``return` `x ` ` `  `# Driver code ` `a, b, c ``=` `2``, ``4``, ``4` `print``(getMinNum(a, b, c)) ` ` `  `# This code is contributed by ` `# Mohit kumar 29 `

C#

 `// C# implementation of the approach ` `using` `System; ` ` `  `class` `GFG ` `{ ` `    ``// Function to return the required number ` `    ``static` `int` `getMinNum(``int` `a, ``int` `b, ``int` `c) ` `    ``{ ` ` `  `        ``// If doesn't belong to the range ` `        ``// then c is the required number ` `        ``if` `(c < a || c > b) ` `        ``{ ` `            ``return` `c; ` `        ``} ` ` `  `        ``// Else get the next multiple of c ` `        ``// starting from b + 1 ` `        ``int` `x = ((b / c) * c) + c; ` ` `  `        ``return` `x; ` `    ``} ` ` `  `    ``// Driver code ` `    ``static` `public` `void` `Main () ` `    ``{ ` `        ``int` `a = 2, b = 4, c = 4; ` `        ``Console.WriteLine( getMinNum(a, b, c)); ` `    ``} ` `} ` ` `  `// This Code is contributed by ajit..  `

PHP

 ` ``\$b``)  ` `        ``return` `\$c``;  ` ` `  `    ``// Else get the next multiple of c  ` `    ``// starting from b + 1  ` `    ``\$x` `= (``floor``((``\$b` `/ ``\$c``)) * ``\$c``) + ``\$c``;  ` ` `  `    ``return` `\$x``;  ` `}  ` ` `  `// Driver code  ` `\$a` `= 2; ` `\$b` `= 4; ` `\$c` `= 4;  ` ` `  `echo` `getMinNum(``\$a``, ``\$b``, ``\$c``);  ` ` `  `// This code is contributed by Ryuga ` `?> `

Output:

```8
```

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.