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


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


for (i = 0; i < 10; i++) {
   sum = sum + *(ptr + i); 
}

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

#include<iostream>
using namespace std;

int main() {

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

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

    ptr = arr;
    for (i = 0; i < 5; i++) {
        sum = sum + *(ptr + i);
    }
    cout << "Sum of array elements:" << sum;

    return 0;
}

Output:

Enter 5 numbers:10 20 30 40 50
Sum of array elements:150

Xiith is created for educational, experimental, and schooling purpose. Examples on Xiith are made easier to make a better or basic understanding. Tutorials, testimonials, and examples are continuously checked to avoid delusion, but we cannot take complete responsibility of all programs on Xiith. When you are operating this site, you have to agree to read and accept our terms of use, cookie and privacy policy.Copyright © 2020 by Xiith. All Rights Reserved. About us.