C++ Program to find sum of array elements using pointers


In this program, You will learn how to find sum of array elements using pointers in C++.


for (i = 0; i < 10; i++) {

sum = sum + *(ptr + i); }


Example: How to find sum of array elements using pointers in C++

Run it Now
#include<iostream>
using namespace std;

int main() {

    int arr[5], i, sum = 0;
    int *ptr;

    cout << "Enter any 5 Numbers :";
    for (i = 0; i < 5; i++) {
        cin >> arr[i];
    }

    ptr = arr;
    for (i = 0; i < 5; i++) {
        sum = sum + *(ptr + i);
    }
    cout << "\nSum of Array Elements :" << sum;

    return 0;
}

Output:

Enter any 5 Numbers :10 20 30 40 50
Sum of Array Elemnets :150