Array Sum in C++

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

Lists in Python

Introduction: In the realm of Python, lists stand as one of the most versatile and widely used data structures. A list is an ordered, muta...

Post Count

Loading...