This program calculates the sum of all elements in an array.
#include <iostream>
int main() {
int n;
std::cout << "Enter the number of elements: ";
std::cin >> n;
int arr[n], sum = 0;
std::cout << "Enter " << n << " elements: ";
for (int i = 0; i < n; ++i) {
std::cin >> arr[i];
sum += arr[i];
}
std::cout << "Sum of elements: " << sum << std::endl;
return 0;
}
No comments:
Post a Comment