C++ Program to Find Sum and Product of Array Elements

This is a C++ Program to Calculate Product and Sum of all Elements in an Array.

Problem Description

The program takes an array of elements and calculates the sum and product of all elements of the array.

Problem Solution

1. The program takes an array of elements and stores them in an array.
2. Using a for loop, the sum and product of the array are calculated.
3. The result is printed.
4. Exit.

C++ Program/Source code

Here is the source code of C++ Program to Calculate Product and Sum of all Elements in an Array. The program output is shown below.

  1. #include<iostream>
  2. using namespace std;
  3. int main ()
  4. {
  5.     int arr[10], n, i, sum = 0, pro = 1;
  6.     cout << "Enter the size of the array : ";
  7.     cin >> n;
  8.     cout << "\nEnter the elements of the array : ";
  9.     for (i = 0; i < n; i++)
  10.     cin >> arr[i];
  11.     for (i = 0; i < n; i++)
  12.     {
  13.         sum += arr[i];
  14.         pro *= arr[i];
  15.     }
  16.     cout << "\nSum of array elements : " << sum;
  17.     cout << "\nProduct of array elements : " << pro;
  18.     return 0;
  19. }
Program Explanation

1. The user is initially asked to enter the size of the array and the value is stored in ‘n’.
2. The variables ‘sum’ and ‘pro’ are initialized as 0 and 1 respectively.
3. The user is asked to enter the array elements. Using a for loop, the elements are stored in the array ‘arr’.
4. Taking a for loop and initializing ‘i’ as 0, the sum and product are calculated and stored in sum and pro respectively.
5. i is incremented in every iteration. The loop continues till i is less than n.
6. The result is then printed.

advertisement
Runtime Test Cases
Case 1 :
Enter the size of the array : 5
Enter the elements of the array : 1 2 3 4 5
Sum of array elements : 15
Product of array elements : 120
 
Case 2 :
Enter the size of the array : 11
Enter the elements of the array : 11 10 9 8 7 6 5 4 3 2 1
Sum of array elements : 66
Product of array elements : 39916800
 
Case 3 :
Enter the size of the array : 3
Enter the elements of the array : 25 75 0
Sum of array elements : 100
Product of array elements : 0

Sanfoundry Global Education & Learning Series – C++ Programs.

To practice all C++ programs, here is complete set of 1000+ C++ Programming examples.

⚡ Claim Your Free Java Certification - December 2025

advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 20s–40s and exploring new directions in your career, I also offer mentoring. Learn more here.