## Place Your Query

# C program to find sum of n numbers

Hello, this is Milind Bhatt. Here I’m going to show you how to write a simple C program to find the sum of n numbers. This is a common problem that you might encounter in your programming journey, and it can be solved using a simple algorithm. Let’s get started!

First, we need to declare some variables that we will use in our program. We need a variable to store the value of n, which is the number of terms we want to add. We also need a variable to store the sum of the terms, which we will initialize to zero. And we need a variable to store the current term, which we will use in a loop.

Next, we need to ask the user to enter the value of n. We can use the scanf function to read the input from the keyboard and store it in our n variable. We also need to check if the input is valid, that is, if it is a positive integer. If not, we can print an error message and exit the program.

Then, we need to write a loop that will iterate from 1 to n and add each term to the sum. We can use a for loop for this purpose, and assign the loop variable i to our current term variable. Inside the loop, we simply add the current term to the sum and update it.

Finally, we need to print the result of our calculation. We can use the printf function to display the sum on the screen.

That’s it! We have written a C program to find the sum of n numbers using a simple algorithm. Here is the complete Algorithm and code for your reference:

## Algorithm

Algorithm for C program to find sum of n numbers

- Step 1: Declare and initialize variables n, sum, and i.
- Step 2: Prompt the user to enter the value of n.
- Step 3: Read the value of n from the user.
- Step 4: Use a looping statement to iterate from 1 to n.
- Step 4.1: Add the current value of i to the sum.

- Step 5: Display the value of sum.

## Program to find sum of n numbers

## C program to find sum of n numbers using for loop

```
//C program to find sum of n numbers using for loop
// C Program@EzyLearning.in
#include <stdio.h>
int main()
{
int n, sum = 0, term;
printf("Enter the value of n: ");
scanf("%d", &n);
if (n <= 0)
{
printf("Invalid input.\n");
return 1;
}
for (int i = 1; i <= n; i++)
{
printf("Enter the value for Term no %d : ");
scanf("%d", &term);
sum = sum + term;
}
printf("The sum of given %d numbers is %d.\n", n, sum);
return 0;
}
```

#### Output

```
Enter the value of n: 5
Enter the value for Term no 1 : 10
Enter the value for Term no 2 : 20
Enter the value for Term no 3 : 30
Enter the value for Term no 4 : 40
Enter the value for Term no 5 : 50
The sum of the given 5 numbers is 150.
--------------------------------
Process exited after 16.05 seconds with return value 0
Press any key to continue . . .
```

## C program to find sum of n numbers using while loop

```
//C program to find sum of n numbers using while loop
// Program@EzyLearning.in
#include <stdio.h>
int main()
{
int n, sum = 0, term;
printf("Enter the value of n: ");
scanf("%d", &n);
if (n <= 0)
{
printf("Invalid input.\n");
return 1;
}
int i = 1;
while ( i <= n )
{
printf("Enter the value for Term no %d : ",i);
scanf("%d", &term);
sum = sum + term;
i++;
}
printf("The sum of given %d numbers is %d.\n", n, sum);
return 0;
}
```

#### Output

```
Enter the value of n: 5
Enter the value for Term no 1 : 10
Enter the value for Term no 2 : 20
Enter the value for Term no 3 : 30
Enter the value for Term no 4 : 40
Enter the value for Term no 5 : 50
The sum of the given 5 numbers is 150.
--------------------------------
Process exited after 16.05 seconds with return value 0
Press any key to continue . . .
```

## C program to find sum of n numbers using do…while loop

```
//C program to find sum of n numbers using do...while loop
// Program@EzyLearning.in
#include <stdio.h>
int main()
{
int n, sum = 0, term;
printf("Enter the value of n: ");
scanf("%d", &n);
if (n <= 0)
{
printf("Invalid input.\n");
return 1;
}
int i = 1;
do
{
printf("Enter the value for Term no %d : ",i);
scanf("%d", &term);
sum = sum + term;
i++;
}while ( i <= n );
printf("The sum of given %d numbers is %d.\n", n, sum);
return 0;
}
```

#### Output

```
Enter the value of n: 2
Enter the value for Term no 1 : 4
Enter the value for Term no 2 : 5
The sum of given 2 numbers is 9.
--------------------------------
Process exited after 5.508 seconds with return value 0
Press any key to continue . . .
```

## C program to find sum of n numbers using function

```
//C program to find sum of n numbers using funtion
// Program@EzyLearning.in
#include <stdio.h>
int main()
{
int n;
printf("Enter the value of n: ");
scanf("%d", &n);
int sum= doSum(n);
printf("The sum of given %d numbers is %d.\n", n, sum);
return 0;
}// end main
int doSum(int count)
{int sum=0, term;
if (count <= 0)
{
printf("Invalid input.\n");
return 1;
}
int i = 1;
do
{
printf("Enter the value for Term no %d : ",i);
scanf("%d", &term);
sum = sum + term;
i++;
}while ( i <= count );
return sum;
}// end funtion
```

#### Output

```
Enter the value of n: 4
Enter the value for Term no 1 : 40
Enter the value for Term no 2 : 45
Enter the value for Term no 3 : 67
Enter the value for Term no 4 : 43
The sum of given 4 numbers is 195.
--------------------------------
Process exited after 20.68 seconds with return value 0
Press any key to continue . . .
```

## Conclusion

Congratulations! You have successfully written a C program to find sum of n numbers using simple for() loop, while() loop, do…while() loop and function*statement. I hope you enjoyed this blog post and learned something new. If you have any questions or feedback, please leave a comment below.*

Thank you for reading!

**EzyLearning!! Happy Learning!!**