Author : Rahul
Write a program to calculate the sum of n natural numbers.
Program
using System;
namespace ProgrammingQuestion
{
class Program
{
static void Main(string[] args)
{
//Variable Declaration
string numberVal;
int number, result;
//User Input
Console.Write("Enter Number of terms whose sum needs to be calculated:");
numberVal = Console.ReadLine();
number = Convert.ToInt32(numberVal);
//Sum of Natural Number Logic
result = (number * (number + 1)) / 2;
//Print Result
Console.WriteLine($"Sum of {number} natural number is: {result}");
Console.Read();
}
}
}
Output
Enter Number of terms whose sum needs to be calculated:10
Sum of 10 natural number is: 55