Write C++ program to Sum of Array Elements using Pointers


I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

Introduction

I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.


  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5.  
  6. int arr[5], i, sum = 0;
  7. int *ptr;
  8.  
  9. cout << "Enter any 5 numbers :";
  10. for (i = 0; i < 5; i++) {
  11. cin >> arr[i];
  12. }
  13.  
  14. ptr = arr;
  15. for (i = 0; i < 5; i++) {
  16. sum = sum + *(ptr + i);
  17. }
  18.  
  19. cout << "\nSum of array elements :" << sum;
  20.  
  21. return 0;
  22.  
  23. }
  24.  

Result

C++ language techstudy.org

     





Related Topics




Popular Posts