Calculate sum of array elements C++ Program

Simple C++ program to calculate sum of array elements. To calculate sum of array elements, first we declare a variable sum and initialize it to 0 and then add array elements one by one into sum.

/* C++ Program to calculate sum of array elements */
#include<iostream>
using namespace std;
int main()
{
 int size;
 cout<<"Enter array isze: ";
 cin>>size;
 
 int arr[size],sum=0;
 
 cout<<"Enter "<<size<<" elements: ";
 for(int i=0;i<size;i++)
 {
  cin>>arr[i];
 }
 
 cout<<"Sum of array elements: ";
 for(int i=0;i<size;i++)
 {
  sum=sum+arr[i];
 }
 cout<<sum;
 
 return 0;
}

Popular posts from this blog