Find Prime number just less than and just greater each element of given Array | Java program for the above approach ; Utility function to check for primality of a number X by checking whether X has any factors other than 1 and itself . ; Factor found ; Function to print primes just less than and just greater than of each element in an array ; Traverse the array ; Traverse for finding prime just less than A [ i ] ; Prime just less than A [ i ] found ; Traverse for finding prime just greater than A [ i ] ; Prime just greater than A [ i ] found ; Driver code ; Input ; Function call
import java . io . * ; class GFG { static boolean isPrime ( int X ) { for ( int i = 2 ; i * i <= X ; i ++ ) if ( X % i == 0 ) return false ; return true ; } static void printPrimes ( int A [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = A [ i ] - 1 ; ; j -- ) { if ( isPrime ( j ) ) { System . out . print ( j + " ▁ " ) ; break ; } } for ( int j = A [ i ] + 1 ; ; j ++ ) { if ( isPrime ( j ) ) { System . out . print ( j + " ▁ " ) ; break ; } } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int A [ ] = { 17 , 28 } ; int N = A . length ; printPrimes ( A , N ) ; } }