This is a C++ Program to Calculate Product and Sum of all Elements in an Array.
The program takes an array of elements and calculates the sum and product of all elements of the array.
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.
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<iostream>
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;
}
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.
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.
- Practice Programming MCQs
- Apply for C++ Internship
- Apply for Computer Science Internship
- Practice Computer Science MCQs
- Check Programming Books