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