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
#include <iostream> NEW_LINE using namespace std ; 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 ; } int main ( ) { int N = 2 ; cout << summation ( N ) ; return 0 ; }