English

Write a program in C++ to read the array of 10 integers. Find and print the sum of all the elements of given array. - Computer Science 1

Advertisements
Advertisements

Question

Write a program in C++ to read the array of 10 integers. Find and print the sum of all the elements of given array.

Code Writing
Advertisements

Solution

#include <iostream>

int main() {
    const int N = 10;
    int arr[N];
    long long sum = 0;

    std::cout << "Enter 10 integers:\n";
    for (int i = 0; i < N; ++i) {
        // Basic input check: ensures the user enters a valid number
        if (!(std::cin >> arr[i])) {
            std::cerr << "Invalid input\n";
            return 1;
        }
        sum += arr[i];
    }

    std::cout << "Sum = " << sum << '\n';
    return 0;
}
shaalaa.com
  Is there an error in this question or solution?
2023-2024 (July) Official Board Paper
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×