# C Program to Find Sum of N Numbers

Howdy readers, today you will learn how to write a program to find the sum of N numbers using the C Programming language.

The below program prompts the user to enter total number of integers to add, then this program find the sum of all integers using the following methods:

1. Using For Loop
2. Using While Loop
3. Using Do While Loop
4. Using User-defined Function
5. Using Recursion
6. Integers Entered by the User

So, without any delay, let’s begin this tutorial.

## C Program to Find Sum of N Numbers

C Program

```// C Program to Find Sum of N Numbers
#include <stdio.h>
int main(){
int num, sum = 0;

// Taking input
printf("Enter the no. of integers to add: ");
scanf("%d", &num);

// Calculating the sum
for (int i = 1; i <= num; i++){
sum = sum + i;
}

// Displaying result
printf("Sum of %d natural numbers: %d", num, sum);

return 0;
}
```

Output

```Enter the no. of integers to add: 25
Sum of 25 natural numbers: 325
```

Explanation

```    int num, sum = 0;
```

In the above program, we have declared two int data type variables named `num` and `sum`. The `sum` variable has assigned a value of 0.

```    // Taking input
printf("Enter the no. of integers to add: ");
scanf("%d", &num);
```

We take input from the user. This number gets stored in the ‘`num`’ named variable.

```    // Calculating the sum
for (int i = 1; i <= num; i++){
sum = sum + i;
}
```

For every iteration, we add the value of `i` with `sum`. After each iteration, the value of `i` gets incremented by 1. This loop keeps executing until `i` is equal to or less than the entered integer `num`.

This gives us the sum of all numbers lying from 1 to the entered integer.

```    // Displaying result
printf("Sum of %d natural numbers: %d", num, sum);
```

The sum of N numbers is displayed on the screen using printf() function.

## C Program to Find Sum of N Numbers Using While Loop

C Program

```// C Program to Find Sum of N Numbers Using While Loop
#include <stdio.h>
int main(){
int num, sum = 0;

// Taking input
printf("Enter the no. of integers to add: ");
scanf("%d", &num);

int i = 1;
while (i <= num){
sum = sum + i;
i++;
}

// Displaying result
printf("Sum of %d numbers: %d", num, sum);

return 0;
}
```

Output

```Enter the no. of integers to add: 12
Sum of 12 numbers: 78
```

Explanation

```    while (i <= num){
sum = sum + i;
i++;
}
```

Within the while loop, for each iteration we add the `sum` with `i`. After each iteration, the value is incremented by 1 until `i` is less than or equal to `num`.

## C Program to Find Sum of N Numbers Using Do While Loop

C Program

```// C Program to Find Sum of N Numbers Using Do While Loop
#include <stdio.h>
int main(){
int num, sum = 0;

// Taking input
printf("Enter the no. of terms: ");
scanf("%d", &num);

int i = 1;

// Computing the sum
do {
sum = sum + i;
i++;
} while (i <= num);

// Displaying result
printf("Sum of %d numbers: %d", num, sum);

return 0;
}
```

Output

```Enter the no. of terms: 5
Sum of 5 numbers: 15
```

Explanation

```    // Computing the sum
do {
sum = sum + i;
i++;
} while (i <= num);
```

The logic is the same as that of the above two programs, this loop keeps executing until `i <= num`.

## C Program to Find Sum of N Numbers Using Functions

C Program

```// C Program to Find Sum of N Number Using Function
#include <stdio.h>
int findSum(int);

int main(){
int num, result;

// Taking input
printf("Enter total no. of integers: ");
scanf("%d", &num);

// Calling out function
result = findSum(num);

// Displaying result
printf("Sum of %d numbers: %d", num, result);

return 0;
}

int findSum(int n){
int i = 1, sum = 0;

while(i <= n){
sum = sum + i;
i++;
}
return sum;
}
```

Output

```Enter total no. of integers: 7
Sum of 7 numbers: 28
```

Explanation

```int findSum(int n){
int i = 1, sum = 0;

while(i <= n){
sum = sum + i;
i++;
}
return sum;
}
```

In the above program, we have defined a custom function named `findSum` which computes and returns the sum using a simple do while loop.

Then, we call this custom function in the main function and print the sum on the screen.

## C Program to Find Sum of N Number Using Recursion

C Program

```// C Program to Find Sum of N Numbers Using Recursion
#include <stdio.h>
int calculateSum(int n);

int main(){

// Taking input
printf("Enter total no. of numbers: ");
scanf("%d", &num);

// Calling out recursive function

// Displaying result
printf("Sum of %d numbers: %d", num, answer);

return 0;
}

int calculateSum(int n){
if (n == 0)
return n;
else
return (n + calculateSum(n - 1));
}
```

Output

```Enter total no. of numbers: 10
Sum of 10 numbers: 55
```

Explanation

```int calculateSum(int n){
if (n == 0)
return n;
else
return (n + calculateSum(n - 1));
}
```

In this program, we have defined a recursive function named `calculateSum` which finds the sum by calling out the recursive function.

## C Program to Find Sum of N Number When Integers are Entered by the User

C Program

```// C Program to Find Sum of N Numbers When Integers are Entered By the User
#include <stdio.h>
int main(){
int num, n, sum = 0;

// Taking input
printf("Enter total no. of terms: ");
scanf("%d", &num);
printf("Enter the elements: \n");
for (int i = 0; i < num; i++){
scanf("%d", &n);
sum = sum + n;
}

// Displaying result
printf("Sum = %d", sum);

return 0;
}
```

Output

```Enter total no. of terms: 3
Enter the elements:
6
9
45
Sum = 60
```

## Conclusion

I hope after reading this tutorial, you understand how to write a program to find the sum of n numbers using C Programming language.

If you have any queries regarding the tutorial, then let us know in the comment section. We will be pleased to solve all of your queries.