Find Prime number just less than and just greater each element of given Array | Utility function to check for primality of a number X by checking whether X haACCs any factors other than 1 and itself . ; if ( X % i == 0 ) 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 ; Input ; Function call
function isPrime ( X ) { for ( let i = 2 ; i * i <= X ; i ++ ) return false ; return true ; } function printPrimes ( A , N ) { for ( let i = 0 ; i < N ; i ++ ) { for ( let j = A [ i ] - 1 ; ; j -- ) { if ( isPrime ( j ) ) { document . write ( j + " " ) ; break ; } } for ( let j = A [ i ] + 1 ; ; j ++ ) { if ( isPrime ( j ) ) { document . write ( j + " " ) ; break ; } } document . write ( " " ) ; } } let A = [ 17 , 28 ] ; let N = A . length ; printPrimes ( A , N ) ;