
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Sum of Elements in a Given Array in C++
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; }
Advertisements