Friday, March 5, 2021
Home C / C++ Array Elements summation in C++

Array Elements summation in C++

Array elements summation is a famous task for programmers, as We can take the array elements from the user also, Similarly we can compute sum of element of array also. Here is an example which is able of compute sum of the elements of an array:

    // Array Elements summation in C++
    #include <iostream>
    using namespace std;
    int main()
    {
     
        const int arraySize = 10; // constant variable indicating size of array
     
        int a[ arraySize ] = { 87, 68, 94, 100, 83, 78, 85, 91, 76, 87 };
     
        int total = 0;
     
        // sum contents of array a
     
        for ( int i = 0; i < arraySize; ++i )
        {
            total += a[ i ];
        }
        cout << "Sum of array elements: " << total << endl;
    }

In the above example we created and initialized an array and now we are going to compute sum of elements of this array. We defined a variable “total” and then we applied a for loop in order to get access to every element of the array, then we added every element into the “total” and at the end we have the computed sum.  Now after the loop when  we compute sum and displayed it on the display.

 

- Advertisment -

Most Popular

Android Webview Tutorial With Example | How to Use Webview in Android

Android Webview Tutorial With Example Have you seen an Android app displaying Web content using Webview. This Android Webview Tutorial is about integrating Webview using Android Studio....

Student Registration Project in PHP

In this Student Registration Project we are providing a form in simple html to the student and that form sends the data to "student.php"...

Java GUI Calculator Source Code

Here we have a Java GUI Calculator Source Code for you in which a Java GUI calculator has been created using "swing" and "awt". The calculator has...

Numeric Array in PHP

Numeric arrays can store numbers, strings and any object but their index will be represented by numbers. By default array index starts from zero....