Check if a number is sandwiched between primes | C # 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 Code
using System ; class GFG { static bool 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 bool isSandwitched ( int n ) { return ( isPrime ( n - 1 ) && isPrime ( n + 1 ) ) ; } public static void Main ( ) { int n = 642 ; Console . Write ( n + " ▁ : ▁ " ) ; if ( isSandwitched ( n ) ) Console . WriteLine ( " Yes " ) ; else Console . Write ( " No " ) ; n = 9 ; Console . Write ( n + " ▁ : ▁ " ) ; if ( isSandwitched ( n ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }