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.
#include
using namespace std;
int main ()
{
int arr[10], n, i, sum = 0, pro = 1;
cout << "Enter the size of the array : ";
cin >> n;
cout << "\nEnter the elements of the array : ";
for (i = 0; i < n; i++)
cin >> arr[i];
for (i = 0; i < n; i++)
{
sum += arr[i];
pro *= arr[i];
}
cout << "\nSum of array elements : " << sum;
cout << "\nProduct of array elements : " << pro;
return 0;
}
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