Program to find sum of first n natural numbers | Efficient C # program to find the sum of first n natural numbers that avoid overflow if result is going to be within limits . ; Returns sum of first n natural numbers ; If n is odd , ( n + 1 ) must be even ; Driver code
using System ; class GFG { static int findSum ( int n ) { if ( n % 2 == 0 ) return ( n / 2 ) * ( n + 1 ) ; else return ( ( n + 1 ) / 2 ) * n ; } public static void Main ( ) { int n = 5 ; Console . Write ( findSum ( n ) ) ; } }