C# Program to Find the Sum of First N Natural Numbers



We are given a number N, and we need to calculate the sum of the first N natural numbers. In this article, we are going to learn how we can find the sum of the first N natural numbers in C#.

Example 1

Input:

 N = 3

Output:

 6

Explanation ?

The first 3 natural numbers are: 1, 2, 3

The sum of these numbers is: 1 + 2 + 3 = 6

Example 2

Input:

N = 5

Output:

15

Explanation ?

The first 5 natural numbers are: 1, 2, 3, 4, 5

The sum of these numbers is: 1 + 2 + 3 + 4 + 5 = 15

Using Iterative Approach

This is a simple and direct approach to find the sum. We use a loop to calculate the sum of each number from 1 to N and add it to a cumulative sum variable.

Steps for Implementation

  • Step 1 : Initialize a variable sum to 0.
  • Step 2 : Loop through each number from 1 to N.
  • Step 3 : For each number, add it to the sum.
  • Step 4 : Return the final value of the sum.

Implementation Code

using System;
class Program {
    static int SumOfNaturalNumbersIterative(int n) {
        int sum = 0;
        for (int i = 1; i <= n; i++) {
            sum += i;
        }
        return sum;
    }
    static void Main() {
        int N = 3;
        int result = SumOfNaturalNumbersIterative(N);
        Console.WriteLine("The sum of the first {0} natural numbers is: {1}", N, result);
    }
}

Output

The sum of the first 3 natural numbers is: 6

Time Complexity

O(N)

Space Complexity

O(1)

Using Formula-Based Approach

In this method, we use a mathematical formula to find the sum without iterating through numbers. The formula for the sum of the first N natural numbers is: Sum = N × (N + 1) / 2

Steps for Implementation

  • Step 1 : Create a function.
  • Step 2 : Inside the function, use the formula to calculate the sum of the first N natural numbers.
  • Step 3 : Return the calculated sum.

Implementation Code

using System;
class Program {
    static int SumOfNaturalNumbersFormula(int n) {
        int sum = n * (n + 1) / 2;
        return sum;
    }
    static void Main() {
        int N = 5;
        int result = SumOfNaturalNumbersFormula(N);
        Console.WriteLine("The sum of the first {0} natural numbers is: {1}", N, result);
    }
}

Output

The sum of the first 5 natural numbers is: 15

Time Complexity

O(1)

Space Complexity

O(1)

Using Recursive Approach

In this approach, we use recursion to find the sum of the first N natural numbers. For each recursive call, we add the current number to the result of the remaining numbers.

Steps for Implementation

  • Step 1 : Define a recursive function SumOfNaturalNumbersRecursive.
  • Step 2 : Define a base case: If n = 0, return 0.
  • Step 3 : For the recursive case, return n + SumOfNaturalNumbersRecursive(n - 1).

Implementation Code

N = 3
using System;
class Program {
    static int SumOfNaturalNumbersRecursive(int n) {
        if (n == 0) return 0;
        return n + SumOfNaturalNumbersRecursive(n - 1);
    }
    static void Main() {
        int N = 4;
        int result = SumOfNaturalNumbersRecursive(N);
        Console.WriteLine("The sum of the first {0} natural numbers is: {1}", N, result);
    }
}

Output

The sum of the first 4 natural numbers is: 10

Time Complexity

O(N)

Space Complexity

O(N)

Real-Life Applications

  • Mathematical Operations: The calculation of sums of numbers is fundamental in many mathematical problems and operations.
  • Scientific Analysis: It is used in physics, chemistry, and other sciences for deriving formulas and solving equations that involve natural numbers.
  • Data Analysis: This type of calculation helps in statistical models, budgeting, and financial forecasting where sums of series are required.
Updated on: 2025-01-30T18:34:05+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements