//Adding all the Elements in an array and displaying their sum
#include<iostream>
using namespace std;
int main(){
int n;
cout<<"Enter the number of Elements in an array: ";
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cin>>a[n];
}
int sum;
for(int i=0;i<n;i++){
sum+=a[n];
}
cout<<sum;
}
Output:
Enter the number of Elements in an array: 2
1
5
10
i replaced ‘i’ with ‘n’ but still wasn’t able to get the desired output. I was expecting that i would get the sum of all the elements of the array!!!
New contributor
Ibrahim Aejaz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.