C Program to find the sum of natural numbers upto N terms

What are Natural numbers?

Integers used for counting are known as natural numbers. Natural numbers start with 1 and goes on to infinity. For example:

The following is a C program to calculate the sum of natural numbers upto n terms:

Expected Output:

1st run:

2nd run:

How it works

The following table demonstrates what happens at each iteration of the for loop, assuming terms = 4.

Iteration sum i
After 1st iteration sum = sum + i = 0+1 = 1 i = 2
After 2nd iteration sum = 1+2 = 3 i = 3
After 3rd iteration sum = 3+3 = 6 i = 4
After 4th iteration sum = 6+4 = 10 i = 5

Recommended Reading:

Leave a Comment

%d bloggers like this: