C Program to find sum of N numbers

In this program, user first enters the count of integers that he wants to add. Then we take N integers as input from user using scanf function inside a for loop and keep on adding it to a variable sum. Once user finished entering N integers we print sum of all N integers on screen. Here we are using addition arithmetic operator('+') to add numbers.

Check this tutorial for detailed explanation of Arithmetic operators.


Importance of Practicing the Program for Finding the Sum of N Numbers for Beginners

  • Understanding of Iteration : Writing this program helps beginners understand the concept of iteration and how to use loops to perform repetitive tasks. It improves their ability to write efficient and concise code.

  • Basic Arithmetic Operations : This program introduces beginners to basic arithmetic operations, such as addition, in C programming. It demonstrates how to perform calculations and store the result.

  • Enhanced Problem-solving Skills : Implementing this program enhances problem-solving skills by challenging beginners to think about how to find the sum of a sequence of numbers using C programming concepts. It improves their ability to analyze and solve mathematical problems.

  • Looping Constructs : Writing this program helps beginners understand looping constructs, such as the for loop, in C programming. It provides hands-on experience with iterating over a sequence of numbers.

  • Preparation for Advanced Topics : The skills and concepts learned from this program prepare beginners for more advanced topics in C programming, such as working with arrays, functions, and complex algorithms. It lays the foundation for understanding more complex programming concepts.

  • Understanding of Variables : Implementing this program enhances beginners' understanding of variables and data types in C programming. It teaches them how to declare and use variables to store data.

C program to calculate sum of N numbers using for loop and without using array

#include <stdio.h>

int main(){
    int numberCount=0, number, counter, sum=0;
    
    printf("Enter the number of integers to add: ");
    scanf("%d",&numberCount);
    
    printf("Enter %d numbers\n",numberCount);
    for(counter = 0; counter < numberCount; counter++){
        scanf("%d", &number);
        sum = sum + number;
    }
    printf("SUM = %d", sum);
    return 0;
}
Output
Enter the number of integers to add: 5
Enter 5 numbers 
1 2 3 4 5
SUM = 15

C program to calculate sum of N numbers using array

In this program we first store all the numbers entered by user in an integer array. Then we traverse this array from index 0 to N-1 and add all numbers using a for loop and '+' operator. In line number 21, we can also uses shorthand assignment operators '+=' for addition. Check this tutorial for detailed explanation of Shorthand assignment operators.

#include <stdio.h>

int main(){
    /* Using array of size 500 to store input numbers */
    int numberCount=0, numbers[500], counter, sum=0;
    
    printf("Enter the number of integers to add: ");
    scanf("%d",&numberCount);
    
    printf("Enter %d numbers\n", numberCount);
    for(counter = 0; counter < numberCount; counter++){
        scanf("%d", &numbers[counter]);
    }
    for(counter = 0; counter < numberCount; counter++){
        sum = sum + numbers[counter];
    }
    
    printf("SUM = %d", sum);
    return 0;
}
Output
Enter the number of integers to add: 7
Enter 5 numbers
7 6 5 4 3 2 1
SUM = 28

C program to calculate sum of N numbers using recursion

We can use recursion to find sum of N numbers by breaking a problem into smaller problem. Function "getSum(int numberCount)" takes numberCount numbers as input and adds them recursively and returns the result to calling function.

#include <stdio.h>
 
int getSum(int numberCount);
 
int main(){
    int numberCount=0, number, counter, sum=0;
   
    printf("Enter the number of integers to add: ");
    scanf("%d",&numberCount);
   
    printf("Enter %d numbers seperated by space \n", numberCount);
    sum = getSum(numberCount);
    printf("SUM = %d", sum);
   
    return 0;
}

int getSum(int numberCount){
    int sum=0;
    /*  exit condition */
    if(0 == numberCount)
        return 0;
    scanf("%d", &sum);
    /* Recursively call getSum for numberCount-1 */
    return sum + getSum(numberCount - 1);
}

Important Points to Remember

  • Data Type Compatibility : Ensure that the data types used for storing numbers are compatible with the operations performed on them.

  • Boundary Conditions : Be mindful of boundary conditions when iterating over a sequence of numbers to avoid errors.

  • Efficient Algorithm : Consider using an efficient algorithm, such as the formula for the sum of an arithmetic sequence, to calculate the sum

Conclusion

In conclusion, the C Program to Find the Sum of N Numbers is a fundamental exercise that introduces beginners to basic arithmetic operations, loops, and variables in C programming. Practicing this program is important for beginners to develop a solid understanding of these concepts, which are essential for more advanced programming tasks.

Related Topics
C Program to add two numbers
C program to add two numbers using pointers
C program to add two complex numbers
C program to find hcf and lcm of two numbers
C Program to find nPr and nCr
C program to swap two numbers
C Program to calculate factorial of a number
List of all C programs