You are on page 1of 3

Danyal Nasir 5112124018

Write a C++ program to perform sequential and binary searches on array of


13 elements.

CODE:

#include <iostream>
using namespace std;
int main() {
int arr[13] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26};
int target;
cout << "Enter the element to search sequentially: ";
cin >> target;
bool foundSequentially = false;
for (int i = 0; i < 13; ++i) {
if (arr[i] == target) {
foundSequentially = true;
cout << "Element found at position " << i << " (Sequential Search)" << endl;
break;
}
}
if (!foundSequentially)
cout << "Element not found (Sequential Search)" << endl;
cout << "Enter the element to search using binary search: ";
cin >> target;
int start = 0, end = 12;
bool foundBinary = false;
while (start <= end) {
int mid = start + (end - start) / 2;
if (arr[mid] == target) {
foundBinary = true;
cout << "Element found at position " << mid << " (Binary Search)" << endl;
break;
} else if (arr[mid] < target) {
start = mid + 1;
} else {
end = mid - 1;
}
}
if (!foundBinary)
cout << "Element not found (Binary Search)" << endl;

return 0;
}

OUTPUT :

You might also like