Write C++ program to find sum of all elements of an array


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. #define MAX_SIZE 100 //Maximum size of the array
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int arr[MAX_SIZE];
  8. int i, num, sum=0;
  9.  
  10. //Reads size and elements in array
  11. cout<<"Enter size of the array: ";
  12. cin>>num;
  13. cout<<"Enter "<<num<< "elements in the array: "<<endl;
  14.  
  15. for(i=0; i<num; i++)
  16. {
  17. cin>>arr[i];
  18. }
  19. //Adding all elements
  20. for(i=0; i<num; i++)
  21. {
  22. sum = sum + arr[i]; // Calculating sum
  23. }
  24. cout<<"Sum of all elements of array: "<< sum;
  25.  
  26. return 0;
  27. }

Result

C++ language techstudy.org

     





Related Topics




Popular Posts