Check if a number is sandwiched between primes | java Program to check whether a number is sandwiched between two primes or not ; returns true if number n is prime ; 0 and 1 both are non - primes ; finding square root of n ; checking if n has any factor upto square root of n if yes its not prime ; Driver 's Code
import java . io . * ; class GFG { static boolean isPrime ( int n ) { if ( n == 0 n == 1 ) return false ; int root = ( int ) Math . sqrt ( n ) ; for ( int i = 2 ; i <= root ; i ++ ) if ( n % i == 0 ) return false ; return true ; } static boolean isSandwitched ( int n ) { return ( isPrime ( n - 1 ) && isPrime ( n + 1 ) ) ; } public static void main ( String [ ] args ) { int n = 642 ; System . out . print ( n + " ▁ : ▁ " ) ; if ( isSandwitched ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; n = 9 ; System . out . print ( n + " ▁ : ▁ " ) ; if ( isSandwitched ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }