-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinearSearch.cpp
More file actions
38 lines (38 loc) · 1.07 KB
/
Copy pathLinearSearch.cpp
File metadata and controls
38 lines (38 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//Linear search//
#include <stdio.h>
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int A[50], num, flag = 0, sizeOfArray, i;
cout << "\nLINEAR SEARCH" << endl;
cout << "\nEnter size of the array(<50): ";
cin >> sizeOfArray;
cout << "Enter elements of an array :\n";
for (i = 0; i < sizeOfArray; i++)
cin >> A[i];
cout << "Enter number to be searched in array : ";
cin >> num;
for (i = 0; i < sizeOfArray; i++)
{
if (num == A[i])
{
flag = 1;
break;
}
}
if (flag == 0)
cout << num << " is not present in array ";
else
if(i+1==1)
cout << num << " is present in array at " << i + 1 << "st position";
else if(i+1==2)
cout << num << " is present in array at " << i + 1 << "nd position";
else if (i+1==3)
cout << num << " is present in array at " << i + 1 << "rd position";
else
cout << num << " is present in array at " << i + 1 << "th position";
getch();
return 0;
}