# C Program to Calculate the Sum of Natural Numbers

In this Post, you will learn how to calculate the sum of natural numbers using the C Programming language.

The Positive numbers 1, 2, 3, . . . which are used for counting and ordering are known as natural numbers

Today, we will write two C Programs to calculate the Sum. First Using For loop statement and Second one using while Loop Statement.

So, without further ado, let’s begin the tutorial.

## C Program to Calculate the Sum of Natural NumbersUsing for Loop

```// C Program To Calculate the Sum of Natural Numbers
#include <stdio.h>
int main(){
int i, n, sum = 0;

printf("Enter a Positive Integer: ");
scanf("%d", &n);

// logic
for (i = 1; i <= n; ++i){
sum = i + sum;
}
printf("The Sum of %d Natural Numbers is: %d.", n, sum);
return 0;
}
```

Output

``````Enter a Positive Integer: 18
The Sum of 18 Natural Numbers is: 171.
``````

## How Does This Program Work ?

``    int i, n, sum = 0;``

In this example, we have declared three integer type variables named as i, n and sum respectively.

``````    // Asking for Input
printf("Enter a Positive Integer: ");
scanf("%d", &n);
``````

Then, the user is asked to enter a Positive Integer. This value will be stored in the n named variable.

``````    // logic
for (i = 1; i <= n; ++i){
sum = i + sum;
}
``````

for loop statement is used to calculate the sum of natural numbers up to n. The value is stored in the sum variable.

``````    printf("The Sum of %d Natural Numbers is: %d.", n, sum);
``````

Finally, the value of the sum is printed using printf() function.

Some of the used terms are as follow:

#include <stdio.h> – In the first line we have used #include, it is a preprocessor command that tells the compiler to include the contents of the stdio.h(standard input and output) file in the program.

The stdio.h is a file which contains input and output functions like scanf() and printf() to take input and display output respectively.

Int main() – Here main() is the function name and int is the return type of this function. The Execution of any Programming written in C language begins with main() function.

scanf() – scanf() function is used to take input from the user.

printf() – printf() function is used to display and print the string under the quotation to the screen.

For loop – A loop is used for initializing a block of statements repeatedly until a given condition returns false.

// – Used for Commenting in C.

## Find the Sum of Natural Numbers Using while Loop

```// C Program To Calculate the Sum of Natural Numbers
#include <stdio.h>
int main(){
int i, n, sum = 0;

printf("Enter a Positive Integer: ");
scanf("%d", &n);

// logic
i = 1;
while (i <= n){
sum = sum + i;
i++;
}
printf("The Sum of %d Natural Numbers is: %d", n, sum);
return 0;
}
```

Output

``````Enter a Positive Integer: 10
The Sum of 10 Natural Numbers is: 55
``````

while Loop – In the while loop, the statements inside the body of the while loop keeps executing until it is evaluated to False.

## Conclusion

I hope after going through this post, you understand how to calculate the sum of Natural Numbers using C Programming language.

If you have any doubts regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.