Construct a Matrix N x N with first N ^ 2 natural numbers for an input N | C # program for the above approach ; Function to print the desired matrix ; Iterate ove all [ 0 , N ] ; If is even ; If row number is even print the row in forward order ; If row number is odd print the row in reversed order ; Driver code ; Given matrix size ; Function call
using System ; class GFG { static void UniqueMatrix ( int N ) { int element_value = 1 ; int i = 0 ; while ( i < N ) { if ( i % 2 == 0 ) { for ( int f = element_value ; f < element_value + N ; f ++ ) { Console . Write ( f + " ▁ " ) ; } element_value += N ; } else { for ( int k = element_value + N - 1 ; k > element_value - 1 ; k -- ) { Console . Write ( k + " ▁ " ) ; } element_value += N ; } Console . WriteLine ( ) ; i = i + 1 ; } } static public void Main ( ) { int N = 4 ; UniqueMatrix ( N ) ; } }