# Write a C# Program to Find the Sum of First N Numbers [3 Methods]

If you are looking to find the sum of first N numbers in C#, check out this complete tutorial. I have explained how to write a C# program to find the sum of first N numbers.

To calculate the sum of the first N numbers in C#, you can write a program that uses a for loop to iterate from 1 to N, adding each number to a cumulative sum. Start by prompting the user to enter the value of N, then use a loop to compute the sum, and finally, display the result. This approach provides a simple and efficient way to perform the calculation in C#.

## Write a C# Program to Find the Sum of First N Numbers

The sum of the first N numbers is the total of all numbers starting from 1 to N. For instance, if N is 5, the sum would be 1 + 2 + 3 + 4 + 5.

Below is a complete example of a C# program that calculates the sum of the first N numbers.

``````using System;

class Program
{
static void Main()
{
Console.WriteLine("Enter the value of N:");

int sum = SumOfNumbers(N);
Console.WriteLine(\$"The sum of the first {N} numbers is: {sum}");
}

static int SumOfNumbers(int n)
{
int sum = 0;
for (int i = 1; i <= n; i++)
{
sum += i;
}
return sum;
}
}
``````
• Reading Input: The program begins by asking the user to enter the value of N.
• Sum Calculation: It then calculates the sum using the SumOfNumbers function.
• For Loop: Inside this function, a for loop runs from 1 to N, adding each number to sum.
• Result Output: Finally, the program displays the total sum.

Here, you can see the output after I ran the code using a Visual Studio C# console application. I enter 10, and then it shows me the output in the screenshot below as 55, as the sum of the first 10 numbers (1 to 10) is 55.

Alternatively, you can also write the C# program like the below:

``````using System;

class Program {
static int SumOfFirstNNumbers(int n) {
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
return sum;
}

static void Main() {
Console.WriteLine("Enter a number:");
Console.WriteLine(\$"The sum of first {number} natural numbers is: {SumOfFirstNNumbers(number)}");
}
}
``````

## Find the Sum of the First N Numbers in C# using the Arithmetic Formula

You can also use an arithmetic formula in C# to find the sum of the first N numbers. Here is a complete code:

``````using System;

class Program {
static int SumOfFirstNNumbers(int n) {
return n * (n + 1) / 2;
}

static void Main() {
Console.WriteLine("Enter a number:");
Console.WriteLine(\$"The sum of first {number} natural numbers is: {SumOfFirstNNumbers(number)}");
}
}
``````

This method is more efficient than the loop method, especially for large numbers, as it does not require iteration.

Once you run the code using Visual Studio, you can see the output in the below screenshot.

## C# program to find sum of first n numbers using recursion

In C#, you can also use the recursion to find the sum of the first N numbers.
Here is a complete program. In this C# program, the function calls itself with n – 1 until n becomes 0. At this point, it starts unwinding and adds each number from 1 to N.

``````using System;

class Program {
static int SumOfFirstNNumbers(int n) {
if (n <= 0) {
return 0;
}
return n + SumOfFirstNNumbers(n - 1);
}

static void Main() {
Console.WriteLine("Enter a number:");