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

MS Excel Logical Functions

Logical functions in Excel are powerful tools that help you make decisions based on conditions. Whether you're comparing values or testi...

Post Count

Loading...