
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Program to find sum of elements in a given array in C++
In this problem, we are given an array arr[] of n integer values. Our task is to create a Program to find sum of elements in a given array in C++.
Program Description − For the given array, we will add up all elements and return the sum.
Let’s take an example to understand the problem
Input
arr[] = {3, 1, 7, 2, 9, 10}
Output
32
Explanation
Sum = 3 + 1 + 7 + 2 + 9 + 10 = 32
Solution Approach
To find the sum of elements of the array, we will traverse the array and extract each element of the array and add them to sumVal which will return the sum.
We can do by two ways,
- Using recursion
- Using iteration
Program to show the implement Recursive approach
Example
#include <iostream> using namespace std; int calcArraySum(int arr[], int n){ if(n == 1){ return arr[n-1]; } return arr[n-1] + calcArraySum(arr, n-1); } int main(){ int arr[] = {1, 4, 5, 7, 6}; int n = sizeof(arr)/ sizeof(arr[0]); cout<<"The sum of elements in a given array is"<<calcArraySum(arr, n); return 0; }
Output
The sum of elements in a given array is 23
Program to show the implement Iterative approach
Example
#include <iostream> using namespace std; int calcArraySum(int arr[], int n){ int sumVal = 0; for(int i = 0; i < n; i++){ sumVal += arr[i]; } return sumVal; } int main(){ int arr[] = {1, 4, 5, 7, 6}; int n = sizeof(arr)/ sizeof(arr[0]); cout<<"The sum of elements in a given array is"<<calcArraySum(arr, n); return 0; }
Output
The sum of elements in a given array is 23
- Related Articles
- C/C++ Program to find the sum of elements in a given array
- Swift Program to Calculate the sum of Elements in a Given Array
- Construct sum-array with sum of elements in given range in C++
- Write a Golang program to calculate the sum of elements in a given array
- How to find the sum of all elements of a given array in JavaScript?
- C Program to find sum of perfect square elements in an array using pointers.
- C++ program to find permutation for which sum of adjacent elements sort is same as given array
- Program to find sum of given sequence in C++
- Write a Golang program to find duplicate elements in a given array
- C# program to print all distinct elements of a given integer array in C#
- Find Sum of all unique subarray sum for a given array in C++
- Program to find minimum elements to add to form a given sum in Python
- Golang Program to find the odd-occurring elements in a given array
- Java program to find the sum of elements of an array
- C++ Program to find Number Whose XOR Sum with Given Array is a Given Number k

Advertisements