C# program to print sum of n natural numbers

Views: 52   

Following program shows you how to print sum of n natural numbers.
In this program we get number from user and shows sum of n natural numbers using following formula
Sum of n natural numbers = input X (input + 1)/2

using System;

class MainClass {
  public static void Main (string[] args) {
    Console.WriteLine("Enter a number:");
    int input = Convert.ToInt32(Console.ReadLine());
    int result = input * (input + 1) / 2;
    Console.WriteLine(result);
  }
}

Output:

Enter a number:
 12
78
On By