Learn Programming and technology with ease @ developerpublish.com

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

# 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:

### You May Also Like

#### C# Program to Display Upper Triangular Matrix

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...

#### C# Program to Demonstrate Bitwise Operators

This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...

#### C# Program to Interchange the Columns of a Matrix

This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...