# Maximum positive integer divisible by C and is in the range [A, B]

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

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

Print -1 if no such number i.e. X exists.

Examples:

```Input: A = 2, B = 4, C = 2
Output: 4
B is itself divisible by C.

Input: A = 5, B = 10, C = 4
Output: 8
B is not divisible by C.
So maximum multiple of 4(C) smaller than 10(B) is 8
```

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

Approach:

• If B is a multiple of C then B is the required number.
• Else get the maximum multiple of C just lesser than B which is the required answer.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the above approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return the required number ` `int` `getMaxNum(``int` `a, ``int` `b, ``int` `c) ` `{ ` ` `  `    ``// If b % c = 0 then b is the ` `    ``// required number ` `    ``if` `(b % c == 0) ` `        ``return` `b; ` ` `  `    ``// Else get the maximum multiple of ` `    ``// c smaller than b ` `    ``int` `x = ((b / c) * c); ` `     `  `    ``if` `(x >= a && x <= b) ` `        ``return` `x; ` `    ``else`  `        ``return` `-1; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `a = 2, b = 10, c = 3; ` `    ``cout << getMaxNum(a, b, c); ` `    ``return` `0; ` `} `

## Java

 `// Java implementation of the above approach ` `import` `java.io.*; ` ` `  `class` `GFG  ` `{ ` `     `  `// Function to return the required number ` `static` `int` `getMaxNum(``int` `a, ``int` `b, ``int` `c) ` `{ ` ` `  `    ``// If b % c = 0 then b is the ` `    ``// required number ` `    ``if` `(b % c == ``0``) ` `        ``return` `b; ` ` `  `    ``// Else get the maximum multiple of ` `    ``// c smaller than b ` `    ``int` `x = ((b / c) * c); ` `     `  `    ``if` `(x >= a && x <= b) ` `        ``return` `x; ` `    ``else` `        ``return` `-``1``; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main (String[] args)  ` `{ ` `    ``int` `a = ``2``, b = ``10``, c = ``3``; ` `    ``System.out.println(getMaxNum(a, b, c)); ` `} ` `} ` ` `  `// This Code is contributed by ajit..  `

## Python3

 `# Python3 implementation of the above approach ` ` `  `# Function to return the required number ` `def` `getMaxNum(a, b, c): ` ` `  `    ``# If b % c = 0 then b is the ` `    ``# required number ` `    ``if` `(b ``%` `c ``=``=` `0``): ` `        ``return` `b ` ` `  `    ``# Else get the maximum multiple  ` `    ``# of c smaller than b ` `    ``x ``=` `((b ``/``/``c) ``*` `c) ` `     `  `    ``if` `(x >``=` `a ``and` `x <``=` `b): ` `        ``return` `x ` `    ``else``: ` `        ``return` `-``1` ` `  `# Driver code ` `a, b, c ``=` `2``, ``10``, ``3` `print``(getMaxNum(a, b, c)) ` ` `  `# This code is contributed  ` `# by Mohit Kumar `

## C#

 `// C# implementation of the above approach ` `using` `System; ` `class` `GFG  ` `{ ` `     `  `// Function to return the required number ` `static` `int` `getMaxNum(``int` `a, ``int` `b, ``int` `c) ` `{ ` ` `  `    ``// If b % c = 0 then b is the ` `    ``// required number ` `    ``if` `(b % c == 0) ` `        ``return` `b; ` ` `  `    ``// Else get the maximum multiple of ` `    ``// c smaller than b ` `    ``int` `x = ((b / c) * c); ` `     `  `    ``if` `(x >= a && x <= b) ` `        ``return` `x; ` `    ``else` `        ``return` `-1; ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main ()  ` `{ ` `    ``int` `a = 2, b = 10, c = 3; ` `    ``Console.WriteLine(getMaxNum(a, b, c)); ` `} ` `} ` ` `  `// This Code is contributed by Code_Mech..  `

## PHP

 `= ``\$a` `&& ``\$x` `<= ``\$b``) ` `        ``return` `\$x``; ` `    ``else` `        ``return` `-1; ` `} ` ` `  `// Driver code ` `\$a` `= 2; ``\$b` `= 10; ``\$c` `= 3; ` `echo``(getMaxNum(``\$a``, ``\$b``, ``\$c``)); ` ` `  `// This Code is contributed ` `// by Mukul Singh ` `?> `

Output:

```9
```

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.