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

Last updated on July 27, 2020

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

```
1, 2, 3, 4, 5, 6, 7, 8, 9, 10 . . .
```

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

terms:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | ```
/**********************************************************
* Program to find the sum of natural numbers upto n terms
***********************************************************/
#include<stdio.h> // include stdio.h
int main()
{
int terms, i;
unsigned long int sum = 0;
printf("Enter the number of terms: ");
scanf("%d", &terms);
for(int i = 1 ; i <= terms; i++)
{
sum += i;
}
printf("%ld", sum);
return 0;
}
``` |

**Expected Output:**

1st run:

1 2 | ```
Enter the number of terms: 4
10
``` |

2nd run:

1 2 | ```
Enter the number of terms: 50000
1250025000
``` |

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

- C Program to find Armstrong numbers
- C Program to find Prime Numbers
- C Program to generate Fibonacci sequence
- C Program to find the sum of the digits of a number until the sum is reduced to a single digit
- C Program to count the number of digits in a number.
- C Program to reverse the digits of a number

Load Comments