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 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:


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:


MORE

C++ Structures

In the C/C++ programming language, the structure is user-defined data types that allow us to store the combination of different data types together. All the …
Read More

C++ Static Keyword

Static s a keyword in C++ that is when used with different types gives different meanings. static keyword can be used with variables in a …
Read More

C++ Templates

Templates in C++ are the powerful feature that allows us to write generic programs (generic classes and generic functions). A single class or function is …
Read More

C++ Friend Function

There are private and protected members in a class that is inaccessible from outside of the class, constituting one of the concepts of object-oriented programming …
Read More

this Pointer in C++

In C++, this is a keyword that refers to the current instance of a class and this pointer holds the address or points to the …
Read More

Inheritance in C++

Inheritance is one of the most important concepts of object-oriented programming. In C++, inheritance is the process of deriving the properties and behaviors of one …
Read More