diff --git a/linear_search.cpp b/linear_search.cpp new file mode 100644 index 0000000000..c18cf3fa1e --- /dev/null +++ b/linear_search.cpp @@ -0,0 +1,32 @@ +#include +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; +}