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

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...