मराठी
महाराष्ट्र राज्य शिक्षण मंडळएचएससी विज्ञान (संगणक विज्ञान) इयत्ता १२ वी

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

प्रश्न

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

कोड लेखन
Advertisements

उत्तर

#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
  या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
2023-2024 (July) Official Board Paper
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×