text: Nearest prime less than given number n | Java program for the above approach ; Function to return nearest prime number ; All prime numbers are odd except two ; It will only be executed when n is 3 ; Driver Code
code: import java . io . * ; class GFG { static int prime ( int n ) { if ( n % 2 != 0 ) n -= 2 ; else n -- ; int i , j ; for ( i = n ; i >= 2 ; i -= 2 ) { if ( i % 2 == 0 ) continue ; for ( j = 3 ; j <= Math . sqrt ( i ) ; j += 2 ) { if ( i % j == 0 ) break ; } if ( j > Math . sqrt ( i ) ) return i ; } return 2 ; } public static void main ( String [ ] args ) { int n = 17 ; System . out . print ( prime ( n ) ) ; } }