Sum of alternating sign Squares of first N natural numbers | C # program to find Sum of alternating sign Squares of first N natural numbers ; Function to calculate the alternating sign sum ; Variable to store the sum ; Loop to iterate each number from 1 to N ; The alternating sign is put by checking if the number is even or odd ; Add the square with the sign ; Add the square with the sign ; Driver code
using System ; class GFG { static int summation ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( i % 2 == 1 ) sum += ( i * i ) ; else sum -= ( i * i ) ; } return sum ; } public static void Main ( ) { int N = 2 ; Console . WriteLine ( summation ( N ) ) ; } }