# C Program to calculate the sum of N natural numbers

April 17, 2022, Learn eTutorial
1206

Natural numbers are normal numbers that we use in counting like 1,2,3,4... which are greater than Zero, in this C program, we need to find the sum of n numbers. For a better understanding, we always recommend you to learn the basic topics of C programming listed below:

## how to find the sum of natural numbers

Natural numbers are the numbers that are greater than zero. In this C program, we need to find the sum of natural numbers starting from '1' up to the number 'n' which is given by the user. For example, suppose the user entered the number '5'. Now we have to add the numbers from '1' to '5' and show the result. 1+2+3+4+5 = 15

## How do we implement the sum of n natural numbers using the C program?

We are doing this program with the help of a  for loop. In this C program after importing the header libraries into the program, we are starting the program with `main` and then we accept the integer number from the user as the value of 'n', using `printf` and `scanf`, then save that into a variable. Initialize Sum = 0 in the beginning. Total Sum is added to the variable 'sum' in each Iteration of the `for` loop, then the output is displayed at the end of the `for` loop.

### The syntax of 'for loop'

``````for (initializationStatement; testExpression; updateStatement)

{

// codes

}
```
```

In the above code, the code inside `for` loop statement is the initialization statement, test expression, and update statement. Here the initialization statement is executed only once. Then the next step is evaluated as the test expression if the test expression is false, '`for` loop' is terminated otherwise the code inside `for` loop is executed and also updates the statement. This process continues until the test expression is false.

For loops are mainly used in programs where the number of iterations is already known or finite. In this C program, we have to find or calculate the sum of natural numbers, which we use `for` loop to calculate.

Note: In mathematics using an arithmetic progression, the equation to find the sum of 'n' integers is given 'Sn = n/2*[2*a+(n-1)*d]' if the numbers given are 1+2+3+4,.......N. Where,

• 'a' is the first term
• 'd' is a common difference.
• 'n' is the number of terms.

## ALGORITHM

STEP 1: Import the header libraries into the program to use built-in functions

STEP 2: Start the program execution using main() function.

STEP 3: Initialize and define the number and sum as zero.

STEP 4: Accept the number from the user using `printf` and `scanf` then save the number in a variable.

STEP 5: Open a `for` loop from 1 to that number and increment by 1 in every iteration.

STEP 6: Add the Sum as Sum + Number in `for` loop iteration.

STEP 7: Print the Sum after all the iterations of the for loop using `printf` in C programming language.

## C Source Code

```                                          ```#include <stdio.h>

void main()
{
int i, N, sum = 0;
printf("Enter an integer number\n");      /* print and gets the value from user */
scanf ("%d", &N);
for (i=1; i <= N; i++)                                 /* beginning of for loop */
{
sum = sum + i;
}
printf ("Sum of first %d natural numbers = %d\n", N, sum);   /* print sum of numbers */
}```
```

## OUTPUT

```RUN1

Enter an integer number
10
Sum of first 10 natural numbers = 55

RUN2

Enter an integer number
50
Sum of first 50 natural numbers = 1275```

VIEW ALL
VIEW ALL