C# Program to Find the Sum of First 50 Natural Numbers using For Loop

This C# program calculates the sum of the first 50 natural numbers. Natural numbers are the positive integers starting from 1.

Problem Statement

Write a C# program that calculates and displays the sum of the first 50 natural numbers. Natural numbers are positive integers starting from 1 and continuing indefinitely. Your program should use a for loop to iterate through the first 50 natural numbers, accumulate their sum, and then output the result.

C# Program to Find the Sum of First 50 Natural Numbers using For Loop

using System;

namespace SumOfFirst50NaturalNumbers
{
    class Program
    {
        static void Main(string[] args)
        {
            int sum = 0;

            // Using a for loop to iterate through the first 50 natural numbers
            for (int i = 1; i <= 50; i++)
            {
                sum += i;
            }

            Console.WriteLine("The sum of the first 50 natural numbers is: " + sum);
        }
    }
}

How it Works

  1. Initialization:
    • We start by declaring an integer variable called sum and initialize it to 0. This variable will be used to store the cumulative sum of the natural numbers.
  2. For Loop:
    • We use a for loop to perform a series of actions repeatedly. In this case, the loop will execute 50 times, starting from i = 1 and ending when i becomes greater than 50.
    • int i = 1; initializes a loop variable i to 1, which is the first natural number.
    • i <= 50 is the loop condition. The loop will continue as long as i is less than or equal to 50.
    • i++ is the increment statement, which increases i by 1 after each iteration.
  3. Loop Body:
    • Inside the loop, we have a single statement: sum += i;. This statement adds the current value of i to the sum variable.
      • In the first iteration, i is 1, so we add 1 to sum.
      • In the second iteration, i is 2, so we add 2 to sum.
      • This process continues until the loop completes all 50 iterations.
  4. Accumulation of Sum:
    • As the loop iterates, the sum variable accumulates the sum of the first 50 natural numbers.
  5. Printing the Result:
    • After the for loop finishes executing, we use Console.WriteLine to display the result.
    • We print a message, “The sum of the first 50 natural numbers is: “, followed by the value of the sum variable, which contains the sum we calculated in the loop.
  6. Program Execution:
    • When you run the program, it initializes sum to 0, enters the for loop, adds the numbers from 1 to 50 to sum, and then displays the final sum in the console.

Input/ Output

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...