This tutorial will guide you on how to write a C program to compute the sum of the first N natural numbers. It's a common problem for beginners that helps you understand basic loops and mathematical operations.

## What are Natural Numbers?

Natural numbers are positive integers beginning with 1 (e.g., 1, 2, 3, and so on). There are **two methods** to calculate the sum of the first N natural numbers:

**Using a loop**: This method involves iterating from 1 to N and adding each number to a running total.**Using a mathematical formula**: This method uses a direct formula for a faster calculation.

## Calculate Sum of N Natural Numbers (Using a Loop)

Here's a simple C program that uses a `for`

loop to calculate the sum of N natural numbers. The program prompts the user to enter a value for N, and then it iterates from 1 to N, adding each number to a running sum.

Example:

```
#include <stdio.h>
int main() {
int n, sum = 0;
// Ask the user for a positive integer input and validate it
do {
printf("Please enter a positive integer: ");
scanf("%d", &n);
if (n <= 0) {
printf("Invalid input. The number must be a positive integer.\n");
}
} while (n <= 0);
// Iterate from 1 to n, adding each number to the sum
for (int i = 1; i <= n; i++) {
sum += i; // Add the current value of i to sum
}
// Print the final result
printf("The sum of the first %d natural numbers is: %d\n", n, sum);
return 0;
}
```

Output:

```
Please enter a positive integer: -3
Invalid input. The number must be a positive integer.
Please enter a positive integer: 5
The sum of the first 5 natural numbers is: 15
```

Explanation:

**User Input**: The program asks the user for a number, which is stored in the variable`n`

.**Input Validation**: The program ensures that the user inputs a positive integer. If the input is invalid (like a negative number or zero), it prompts the user again.**Looping Through Numbers**: The`for`

loop runs from 1 to`n`

. In each iteration, the current number`i`

is added to`sum`

.**Print the Sum**: Once the loop is complete, the program prints the final value of`sum`

.

## Calculate Sum of N Natural Numbers (Using Formula)

Instead of using a loop, you can calculate the sum in one step using a direct formula. This method is faster, especially for larger numbers. The formula for calculating the sum of the first N natural numbers is:

Formula:

`Sum = (N * (N + 1)) / 2`

Example:

```
#include <stdio.h>
int main() {
int n, sum;
// Ask the user for a positive integer input and validate it
do {
printf("Please enter a positive integer: ");
scanf("%d", &n);
if (n <= 0) {
printf("Invalid input. The number must be a positive integer.\n");
}
} while (n <= 0);
// Calculate the sum using the formula
sum = n * (n + 1) / 2;
// Output the result
printf("The sum of the first %d natural numbers is: %d\n", n, sum);
return 0;
}
```

Output:

```
Please enter a positive integer: 0
Invalid input. The number must be a positive integer.
Please enter a positive integer: 10
The sum of the first 10 natural numbers is: 55
```

Explanation:

**User Input**: The user enters a number`n`

.**Input Validation**: The program ensures the input is a valid positive integer before performing the calculation.**Formula Calculation**: The sum is calculated directly using the formula`Sum = (N * (N + 1)) / 2`

.**Print the Sum**: The result is printed immediately.

## Conclusion

This tutorial demonstrated two efficient methods for calculating the sum of the first N natural numbers in C. By understanding both the loop and formula approaches, you can select the best method based on your specific needs and the size of the input.