 # C Program to calculate the sum of N natural numbers

For a better understanding of this C program example, we always recommend you to learn the basic topics of C programming listed below:

## how to find the sum of natural numbers

To solve the sum of n integers, we have to write a C program that uses a `for` loop from 1 to n. 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.

In this C program, we get the value of N from the `scanf` statement. Now we write a `for` loop to find the sum of N numbers starting from 1. 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.

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

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, using `printf` and `scanf`, then save that into a variable, after that, we open a `for` loop from 1 to that number and in each iteration of the loop we add that number to the sum and finally, we print the sum.

## 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
##### OtherTutorials
VIEW ALL 