Program to Calculate Sum of array elements in C

Spread the love
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:

Output:

Prerequisites:

It is recommended to go through the following programs to understand the sum of arrays elements program better.

Sum of array elements in C Program Explanation:

  1. Start the program by declaring an array named bills with the size 10.
  2. 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.
  3. Create a variable sum and initialize it with zero(0).
  4. 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];
  5. Once the above loop is finished the sum variable contains the sum of array elements.
  6. Prin the results on the standard output (console) using the printf function.

Sum of array elements program Algorithm:

  1. Declare an integer array  bills of size  10 and an integer variable i
  2. Prompt the user to input the values for the array
  3. FOR i = 0 to SIZE-1 ( Loop )
    • Read the value for  bills[i]
  4. FOR i = 0 to SIZE-1 ( Loop )
    • sum = sum + bills[i];
  5. Print the sum value on the console

Sum of array elements in C Program:

Let’s convert the above logic into the c program.

Program Output:

Compile and run the program using the GCC(Any) compiler.

sum-of-array-elements-in-c-program-output

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.

Sum of array elements using function program explanation:

  1. Create bills array with the size 10 and also create a sum variable and initialize with zero(0).
  2. 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.
  3. 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];
  4. Call the read() function from the main() function to take user input
  5. Call the sumOfArrayElements function to calculate the sum of array elements. This function returns the sum of elements. Capture it in sum variable.
  6. Print the sum variable on the console.

Program Output:

Let’s compile and run the program using any compiler.

Test Case 1:

sum-of-array-elements-using-functions-in-c-language
  1. C Program to Calculate the Factorial of a Number using Recursion
  2. C Program to generate Fibonacci series using Recursion
  3. C Program to print first N Natural Numbers using Recursion
  4. C Program to calculate the sum of N Natural Numbers using Recursion
  5. C Program to calculate the Product of N Natural Numbers using Recursion
  6. C Program to Count the Number of digits in a Number using Recursion
  7. C Program to Calculate the Sum of Digits using Recursion
  8. C Program to Product the Number of digits in a Number using Recursion
  9. C Program to Reverse a Number using Recursion.
  10. C Program to check whether the number is Palindrome using Recursion
  11. C Program to Calculate LCM using Recursion.

Venkat

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

Leave a Reply