Program to Calculate the Sum of Natural Numbers - (C Programs)
Example
Enter a positive integer: 5
Sum of first 5 natural numbers = 15
Output
The program will output the sum of the first "n" natural numbers entered by the user.
Explanation
This program uses a loop and the formula for the sum of natural numbers to calculate the sum of the first "n" natural numbers entered by the user. The formula is:
sum = (n * (n + 1)) / 2
where "n" is the number of terms to be added. The program obtains the value of "n" from the user, checks if it is a positive integer, and calculates the sum using the formula.
Use
This program can be used to calculate the sum of the first "n" natural numbers, where "n" is any positive integer entered by the user. It can be used in various mathematical and programming applications, such as finding the average of a series of numbers or generating a set of consecutive integers.
Summary
This program calculates the sum of the first "n" natural numbers entered by the user, using a loop and the formula for the sum of natural numbers. It is a simple and useful program for various mathematical and programming applications.