fork download
  1. #include<iostream>
  2. using namespace std;
  3. bool LinearSearch(int arr[], int size, int key){
  4.  
  5. // Base Case
  6. if(size == 0) // If the array is empty
  7. return false;
  8.  
  9. // If the element is present
  10. if (arr[0] == key){
  11. return true;
  12. }
  13. else{
  14. bool remainingPart = LinearSearch(arr+1, size-1, key); // Recursive case
  15.  
  16. // Return true if the element is found in the remaining part of the array
  17. return remainingPart;
  18.  
  19. }
  20.  
  21. }
  22.  
  23. int main(){
  24. int arr[] = {1,2,3,4,5,6};
  25. int size = sizeof(arr)/sizeof(arr[0]); //size of the array
  26. int key = 10;
  27. bool ans = LinearSearch(arr, size, key);
  28. if(ans){
  29. cout << " Key is present in Array" << endl;
  30. }
  31. else{
  32. cout<< "Key is not present in Array" << endl;
  33. }
  34. return 0;
  35.  
  36. }
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
Key is not present in Array