pgrm_007.cpp
Problem Statement
pgrm_007.cpp
Right a program to calculate sum and product of array elements.
Source Code
cpp
#include <iostream>
using namespace std;
class ArrayOperations {
private:
int *arr;
int n;
public:
int i;
ArrayOperations(int size) {
n = size;
arr = new int[n];
}
void getData() {
for (i = 0; i < n; i++) {
cout << "Enter element " << i + 1 << ": ";
cin >> arr[i];
}
}
int add() {
int sum = 0;
for (i = 0; i < n; i++) {
sum += arr[i];
}
return sum;
}
long product() {
long prod = 1;
for (i = 0; i < n; i++) {
prod *= arr[i];
}
return prod;
}
void display() {
cout << "sum = " << add() << endl;
cout << "product = " << product() << endl;
}
~ArrayOperations() {
delete[] arr;
}
};
int main() {
int n;
cout << "Enter size: ";
cin >> n;
ArrayOperations ao(n);
ao.getData();
ao.display();
return 0;
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
● Author - Amit Dutta · Updated - 06 Jul 2026