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 N is an even number ; Update alternateSum ; If N is an odd number ; Update alternateSum ; Driver Code
using System ; class GFG { static int alternatingSumOfFirst_N ( int N ) { int alternateSum = 0 ; if ( N % 2 == 0 ) { alternateSum = ( - N ) / 2 ; } else { alternateSum = ( N + 1 ) / 2 ; } return alternateSum ; } public static void Main ( String [ ] args ) { int N = 6 ; Console . Write ( alternatingSumOfFirst_N ( N ) ) ; } }