Program to Calculate Sum of array elements in C
Program Description:
Write a program to calculate the Sum of array elements in C programming language. The program prompts the user to array elements and calculates the sum of all elements.
Example Input and Output:
Input:
1 2 3 4 5 6 7 8 9 10 11 |
Please input array elements bills[0] : 4 bills[1] : 3 bills[2] : 2 bills[3] : 1 bills[4] : 4 bills[5] : 8 bills[6] : 9 bills[7] : 4 bills[8] : 2 bills[9] : 8 |
Output:
1 |
Sum of Elements of Array is : 45 |
Prerequisites:
It is recommended to go through the following programs to understand the sum of arrays elements program better.
- Introduction to C Arrays – How to Declare, Access, and Modify Arrays
- 2D arrays in C with Example programs
- How to pass arrays to functions in C
Sum of array elements in C Program Explanation:
- Start the program by declaring an array named bills with the size 10.
- Now, Let’s take the input numbers from the user and update the array. Start the for loop from the index zero(0) and go till the size-1 i.e 9. At each iteration prompt the user for an integer number and store the value in bills[i] using the scanf function.
- Create a variable sum and initialize it with zero(0).
- To calculate the sum of all elements of the array. We need to go through array elements using a loop and at each iteration add the value of the array element ( bills[i]) to sum. – sum = sum + bills[i];
- Once the above loop is finished the sum variable contains the sum of array elements.
- Prin the results on the standard output (console) using the printf function.
Sum of array elements program Algorithm:
- Declare an integer array bills of size 10 and an integer variable i
- Prompt the user to input the values for the array
- FOR i = 0 to SIZE-1 ( Loop )
- Read the value for bills[i]
- FOR i = 0 to SIZE-1 ( Loop )
- sum = sum + bills[i];
- Print the sum value on the console
Sum of array elements in C Program:
Let’s convert the above logic into the c program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
/* Program to calculate the sum of array elements sillycodes.com */ #include <stdio.h> int main() { // declare the 'bills' array int bills[10]; int i; // Read the input from the user printf("Please input array elements \n"); for(i = 0; i < 10; i++) { printf("bills[%d] : ", i); scanf("%d", &bills[i]); } // Create 'sum' variable with value 'zero' int sum = 0; // calculate the sum of all elements of the array for(i = 0; i < 10; i++) { sum = sum + bills[i]; // sum += bills[i] } // print the result printf("Sum of Elements of Array is : %d\n", sum); return 0; } |
Program Output:
Compile and run the program using the GCC(Any) compiler.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
$ gcc sum-of-array-prog.c $ ./a.out Please input array elements bills[0] : 4 bills[1] : 3 bills[2] : 2 bills[3] : 1 bills[4] : 4 bills[5] : 8 bills[6] : 9 bills[7] : 4 bills[8] : 2 bills[9] : 8 Sum of Elements of Array is : 45 $ ./a.out Please input array elements bills[0] : 98 bills[1] : 23 bills[2] : 48 bills[3] : 23 bills[4] : 98 bills[5] : 45 bills[6] : 65 bills[7] : 43 bills[8] : 98 bills[9] : 23 Sum of Elements of Array is : 564 $ |
As we can see from the above output, the Program is giving the desired results.
Sum of array elements using a function in C Program:
Let’s divide the above program and create the functions for each specific task. It is good idea to use the functions which does the specific task. So let’s create two functions read and sumOfArrayElements to read the input values and calculate the sum of array elements respectively.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
/* Program to calculate the sum of array elements using function sillycodes.com */ #include <stdio.h> /** * @brief - Read the elements from INPUT * * @param bills - 'bills' Array * @param size - size of the array */ void read(int *bills, int size) { int i; printf("Please input the Array elements\n"); // Read the input from the user for(i = 0; i < size; i++) { printf("bills[%d] : ", i); scanf("%d", &bills[i]); } } /** * @brief - Sum of all array elements of the 'bills' array * * @param bills - 'bills' array * @param size - 'bills' array 'size' * @return int - 'sum' of all elements of array */ int sumOfArrayElements(int *bills, int size) { // Create 'sum' variable with value 'zero' int sum = 0, i; // calculate the sum of all elements of the array for(i = 0; i < size; i++) { sum = sum + bills[i]; // sum += bills[i] } // return the 'sum' return sum; } int main() { // declare the 'bills' array int bills[10]; int i, sum = 0; // Call 'read' function to Read the input from the user read(bills, 10); // call the 'sumOfArrayElements' function to calculate sum of elements sum = sumOfArrayElements(bills, 10); // print the result printf("Sum of all Elements of Array is : %d\n", sum); return 0; } |
Sum of array elements using function program explanation:
- Create bills array with the size 10 and also create a sum variable and initialize with zero(0).
- Define
read() function to take the input numbers from the user. The
read function takes
*bills and
size as formal arguments
- The read function uses a for loop to prompt the user for input and update the array elements.
- Define another function
sumOfArrayElements to calculate the sum of all array elements.
- The sumOfArrayElements functions take integer pointer *bills and array size size as the formal arguments.
- This function iterates over the bills and at each iteration adds the sum and bills[i] – sum = sum + bills[i];
- Call the read() function from the main() function to take user input
- Call the sumOfArrayElements function to calculate the sum of array elements. This function returns the sum of elements. Capture it in sum variable.
- Print the sum variable on the console.
Program Output:
Let’s compile and run the program using any compiler.
Test Case 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
$ gcc sum-of-array-func.c $ ./a.out Please input the Array elements bills[0] : 3 bills[1] : 9 bills[2] : 2 bills[3] : 4 bills[4] : 8 bills[5] : 5 bills[6] : 6 bills[7] : 3 bills[8] : 4 bills[9] : 5 Sum of all Elements of Array is : 49 $ ./a.out Please input the Array elements bills[0] : 32 bills[1] : 54 bills[2] : 65 bills[3] : 12 bills[4] : 9 bills[5] : 87 bills[6] : 67 bills[7] : 54 bills[8] : 34 bills[9] : 86 Sum of all Elements of Array is : 500 $ |
Related Programs:
- C Program to Calculate the Factorial of a Number using Recursion
- C Program to generate Fibonacci series using Recursion
- C Program to print first N Natural Numbers using Recursion
- C Program to calculate the sum of N Natural Numbers using Recursion
- C Program to calculate the Product of N Natural Numbers using Recursion
- C Program to Count the Number of digits in a Number using Recursion
- C Program to Calculate the Sum of Digits using Recursion
- C Program to Product the Number of digits in a Number using Recursion
- C Program to Reverse a Number using Recursion.
- C Program to check whether the number is Palindrome using Recursion
- C Program to Calculate LCM using Recursion.