C++ program to find sum of elements in an array

Finding sum of elements in an array in C++

In this article, we will learn to program to find the sum of elements in an array in C++. For this, we will iterate each element through a for loop and will add each element in the previous sum repeatedly on every iteration. Steps and algorithm for the same is given in detailed below.

Finding sum of elements in an array

Steps to find sum of element in an array in C++

Following steps are followed while finding the sum of element in an array:-

  1. Initialize the required variables.
  2. Accept the inputs from user.
  3. Iterate each element of the array using a for loop.
  4. Add every element that is iterated in any other variable for ex sum.
  5. Print that variable.
C++ program to find sum of elements in an array

Algorithm to find sum of element in an array

  • Start
  • Sum=0
  • For i= 0 to n-1
  • Sum+=arr[i]
  • Exit

C++ programming code to find sum of element in an array

#include <iostream>
using namespace std;
int main()
{
   int arr[100],i,size,sum=0;
   cout<<"Enter the number of elements: ";
   cin>>size;//Accepting array size
   cout<<"Enter the value of elements: "<<endl;
   for(i=0;i<n;i++)
   {
     cin>>arr[i]; //Accepting values
   }

   for(i=0;i<n;i++)
   {
    sum=sum+arr[i];//Calculating sum
   }
    cout<<"Sum of elements in an array is: "<<sum;
    return 0;
}
Output:
Enter the number of elements: 5
Enter the value of elements: 
7
10
3
12
5
Sum of elements in an array is: 37