Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions linear_search.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <iostream>
using namespace std;

int main() {
int n, key;
cout << "Enter number of elements: ";
cin >> n;

int arr[n];
cout << "Enter " << n << " elements: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}

cout << "Enter the element to search: ";
cin >> key;

bool found = false;
for (int i = 0; i < n; i++) {
if (arr[i] == key) {
cout << "Element found at index " << i << endl;
found = true;
break;
}
}

if (!found) {
cout << "Element not found!" << endl;
}

return 0;
}