C/C++ Program to find the sum of elements in a given array


Sum of all Array elements means add all array Elements. Suppose we have 5 Elements in array and we want to find there sum.

arr[0]=1
arr[1]=2
arr[2]=3
arr[3]=4
arr[4]=5

Sum off all above elements are

arr[0]+arr[1]+arr[2]+arr[3]+ arr[4]=1+2+3+4+5=15


Input:1,2,3,4,5
Output:15

Explanation

Using For loop to get to the every index element and taking sum of them

arr[0]+arr[1]+arr[2]+arr[3]+ arr[4]=1+2+3+4+5=15

Example

#include <iostream>
using namespace std;
int main() {
   int i,n,sum=0;
   int arr[]={1,2,3,4,5};
   n=5;
   for(i=0;i<n;i++) {
      sum+=arr[i];
   }
   cout<<sum;
   return 0;
}

Updated on: 19-Aug-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements