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 N = Convert.ToInt32(Console.ReadLine());
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:");
int number = Convert.ToInt32(Console.ReadLine());
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:");
int number = Convert.ToInt32(Console.ReadLine());
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:");
int number = Convert.ToInt32(Console.ReadLine());
Console.WriteLine($"The sum of first {number} natural numbers is: {SumOfFirstNNumbers(number)}");
}
}
Conclusion
Writing a C# program to find the sum of the first N numbers is straightforward, and I have shown here how to write a program to find the sum of first N numbers in C# using various methods like a for loop, an arithmetic formula, and recursion.
You may also like:
- How to Write a Program to Find a Leap Year in C#
- Write A C# Program To Find The Largest Of Three Numbers
- How to Swap Two Numbers in C# Without Using a Third Variable?
- How to Swap Two Numbers in C#?
- Write A C# Program To Print The Multiplication Table Of A Number
Bijay Kumar is a renowned software engineer, accomplished author, and distinguished Microsoft Most Valuable Professional (MVP) specializing in SharePoint. With a rich professional background spanning over 15 years, Bijay has established himself as an authority in the field of information technology. He possesses unparalleled expertise in multiple programming languages and technologies such as ASP.NET, ASP.NET MVC, C#.NET, and SharePoint, which has enabled him to develop innovative and cutting-edge solutions for clients across the globe. Read more…