- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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; }
Advertisements
To Continue Learning Please Login
Login with Google