+ 6

How to create an array of 10 elements & display sum of its elements

7th Oct 2017, 3:00 AM
IMRAN SHAIKH
IMRAN SHAIKH - avatar
3 Respuestas
+ 6
#include <iostream> using namespace std; int main() { int arr[] = {1,2,3,4,5,6,7,8,9,10}; int total = 0; for(int i = 0; i < sizeof(arr)/sizeof(arr[0]); ++i) { total += arr[i]; } cout << total; return 0; }
7th Oct 2017, 3:15 AM
ChaoticDawg
ChaoticDawg - avatar
+ 8
you mean ...from user input?
7th Oct 2017, 3:04 AM
Abhishek Pun
Abhishek Pun - avatar
+ 1
#include<iostream> using namespace std; int main() { int arr[10],i,total=0; cout<<"\nEnter any 10 elements : "; for(i=0;i<10;i++) { cin>>arr[i]; } for(i=0;i<10;i++) { total+=a[i]; } cout<<"\nTotal = "<<total; return 0; }
8th Oct 2017, 4:56 AM
Ritik Singh
Ritik Singh - avatar