-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3_2.cpp
More file actions
41 lines (31 loc) · 962 Bytes
/
3_2.cpp
File metadata and controls
41 lines (31 loc) · 962 Bytes
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
#include <iostream>
using namespace std;
// Recursive function to calculate sum
int recursiveSum(int arr[], int size, int index = 0) {
if (index == size) return 0;
return arr[index] + recursiveSum(arr, size, index + 1);
}
// Iterative function to calculate sum
int iterativeSum(int arr[], int size) {
int total = 0;
for (int i = 0; i < size; ++i) {
total += arr[i];
}
return total;
}
int main() {
int size;
cout << "Enter size of the array: ";
cin >> size;
int* arr = new int[size]; // Dynamic array allocation
cout << "Enter " << size << " elements:\n";
for (int i = 0; i < size; ++i) {
cin >> arr[i];
}
int recSum = recursiveSum(arr, size);
int iterSum = iterativeSum(arr, size);
cout << "Recursive Sum: " << recSum << endl;
cout << "Iterative Sum: " << iterSum << endl;
cout<<"24ce052_pushti kansara";
return 0;
}