Skip to content

Commit 5e8f0cc

Browse files
authored
Merge pull request #18 from Mahesh0911/main
Calculate the sum of elements in an array
2 parents 1ea1ec4 + 5b8ddea commit 5e8f0cc

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

array_sum.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <iostream>
2+
3+
int main()
4+
{
5+
int n;
6+
std::cout << "Enter the number of elements in the array: ";
7+
std::cin >> n;
8+
9+
int myArray[n];
10+
int arraySum = 0;
11+
12+
std::cout << "Enter " << n << " elements, one at a time:\n";
13+
for (int i = 0; i < n; ++i)
14+
{
15+
std::cin >> myArray[i];
16+
arraySum += myArray[i];
17+
}
18+
19+
std::cout << "Sum of elements in the array: " << arraySum << std::endl;
20+
21+
return 0;
22+
}

0 commit comments

Comments
 (0)