Check if Matrix sum is prime or not | C # implementation to check if the sum of matrix is prime or not ; Function to check whether a number is prime or not ; Corner case ; Check from 2 to n - 1 ; Function for to find the sum of the given matrix ; Driver Code
using System ; class GFG { static int N = 4 , M = 5 ; static Boolean isPrime ( int n ) { if ( n <= 1 ) return false ; for ( int i = 2 ; i <= Math . Sqrt ( n ) ; i ++ ) if ( n % i == 0 ) return false ; return true ; } static int takeSum ( int [ ] [ ] a ) { int s = 0 ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < M ; j ++ ) s += a [ i ] [ j ] ; return s ; } public static void Main ( String [ ] args ) { int [ ] [ ] a = new int [ ] [ ] { new int [ ] { 1 , 2 , 3 , 4 , 2 } , new int [ ] { 0 , 1 , 2 , 3 , 34 } , new int [ ] { 0 , 34 , 21 , 12 , 12 } , new int [ ] { 1 , 2 , 3 , 6 , 6 } } ; int sum = takeSum ( a ) ; if ( isPrime ( sum ) ) Console . Write ( " YES " + " STRNEWLINE " ) ; else Console . Write ( " NO " + " STRNEWLINE " ) ; } }