**Sum of N numbers** **in c** program is very easy and simple program. Generally such programs asked in exam. This is the basic program in c.

In this tutorial we will see logic and algorithm to Sum N numbers , how to implement the program in c.

This programs performs sum of the N numbers.

**Example : **

Suppose we have the 4 numbers as 10, 20, 30, 40

Sum of N numbers will add all 4 numbers = 10 + 20 + 10 + 10

Sum of 4 numbers is = 50

**Logic to implement sum of N number program :**

We take the N from user that is how many number user want to add next. After that using for loop we take N number input from user and simultaneously we add number user enters.

**Concept you should know from c :**

**Algorithm :**

- Start
- Declare variables
- Take N number from user
- Take number one by one till N input from user
- Perform addition
- print sum
- End

## C program to sum n numbers

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <stdio.h> int main() { int n, sum = 0, c, value; printf("How many numbers you want to add?\n"); scanf("%d", &n); printf("Enter %d integers\n", n); for (c = 1; c <= n; c++) { scanf("%d", &value); sum = sum + value; } printf("Sum of the integers = %d\n", sum); return 0; } |

**Output for Sum of N numbers in c program:**

**Explanation :**

**First include preprocessor directives stdio.h in program.****Next write main function from where the execution of the program begins.****Declare the variables**. int n => to store N values, sum = 0 => to store addition of N numbers, c => to store iterative value and value => to store the Nth value user enters.**Next take N value user want to add . and store it in n.****Now using for loop take a 4 number from user**and store that input in value now we have value so perform addition and store it in sum.**For example :**Suppose N = 3 now at next statement for (c = 1; c <= n; c++)**At Iteration 1:**c=1 and condition 1<= 3 becomes true and executes the for blockscanf(“%d”, &value) => 10sum = sum + value => sum=10**At Iteration 2:**c=2 and condition 2<= 3 becomes true and executes the for block

scanf(“%d”, &value) => 10

sum = sum + value => sum=20

**At Iteration 3:**c=3 and condition 3<= 3 becomes true and executes the for block

scanf(“%d”, &value) => 10

sum = sum + value => sum=30

**At Iteration 4:**c=4 and condition 4<= 3 becomes false and execution of the for block skipped.

**Now we have the sum of N numbers in sum variable.****So print the value sum=30****End.**

## Leave a Reply