Sum of first N natural numbers with alternate signs | C # program to implement the above approach ; Function to find the sum of first N natural numbers with alternate signs ; Stores sum of alternate sign of first N natural numbers ; If is an even number ; Update alternateSum ; If i is an odd number ; Update alternateSum ; Driver Code
using System ; class GFG { static int alternatingSumOfFirst_N ( int N ) { int alternateSum = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( i % 2 == 0 ) { alternateSum += - i ; } else { alternateSum += i ; } } return alternateSum ; } public static void Main ( String [ ] args ) { int N = 6 ; Console . Write ( alternatingSumOfFirst_N ( N ) ) ; } }