Skip to content
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,8 @@ In order to achieve greater coverage and encourage more people to contribute to
</a>
</td>
<td> <!-- C++ -->
<a href="./CONTRIBUTING.md">
<img align="center" height="25" src="./logos/github.svg" />
<a href="./src/cpp/LinearSearchRecursive.cpp">
<img align="center" height="25" src="./logos/cplusplus.svg" />
</a>
</td>
<td> <!-- Java -->
Expand Down
36 changes: 36 additions & 0 deletions src/cpp/LinearSearchRecursive.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <iostream>

using namespace std;

int linearSearchRecursive(int arr[], int n, int index, int target) {
if (index >= n) {
return -1;
}

if (arr[index] == target) {
return index;
}

return linearSearchRecursive(arr, n, index + 1, target);
}

int main() {
int n;
cout << "Enter The size of array : ";
cin >> n;
int arr[n];
cout << "Enter the element of array : " << endl;
for (int i = 0; i < n; i++) {
cin >> arr[i];
}

int target;
cout << "Enter the element you need to search in the array : ";
cin >> target;
int answer = linearSearchRecursive(arr, n, 0, target);
if (answer >= 0) {
cout << "Target Element Found on index : " << answer << endl;
} else {
cout << "Target Element Not Found" << endl;
}
}