C Program to find the sum of all elements in an Array2 min read

In this section, we will write a C program to perform the addition of all elements in Array.

Before we start, if you want to learn about the Arrays as this program uses an array, click the link below.

Program:

Take the user input for the elements of an array and then by iterating the loop, add each of the element present in an array at each iteration.

Example:

Input: arr[] = {1, 2, 3}
loop: 1 + 2 + 3 = 6
Output: 6

We will write two different programs in C to sum the elements in an array.

  1. Standard method
  2. Using function (user-defined function)

C Program to find the sum of all elements present in an Array

This is a standard method where all of the calculation is done within the main function.

Output:

Enter size of an array: 5
Enter 5 elements in an array:
5
10
6
20
3
Sum of the element of an array: 44


C Program to find the sum of all elements present in an Array using function

A separate function (array_sum()) is created and the parameter array (arr), and the size of an array (size) is passed.

The function calculates the sum of all elements and returns the total sum to the main function where it is displayed.

Output:

Enter size of an array: 4
Enter 5 elements in an array:
5
10
3
8
Sum of the element of an array: 26


MORE

Java Program to find the sum of the Largest Forward Diagonal

in this tutorial, we will write a java program to find the sum of the Largest Forward Diagonal in an Arraylist (matrix). Java Program to …

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a numberĀ using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …