-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinearSearch.java
More file actions
57 lines (54 loc) · 1.44 KB
/
LinearSearch.java
File metadata and controls
57 lines (54 loc) · 1.44 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Program: Linear Search Implementation
// Topic: Searching Algorithms (Arrays)
// Description: Reads five integers into an array and searches for a given element using linear search.
// Displays the position of the element if found, otherwise prints “Element Not Found.” Demonstrates basic array traversal and conditional logic.
package Searching;
import java.util.*;
/**
*
* @author Samim
*/
public class LinearSearch
{
public void input(int ar[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Elements :");
for(int i=0;i<5;i++)
{
ar[i]=sc.nextInt();
}
}
public int searching(int search, int ar[])
{int flag= 0;
for(int i=0;i<5;i++)
{
if(ar[i]==search)
{
flag=i+1;
break;
}
}
return flag;
}
public void display(int flag)
{
if(flag!=0)
{
System.out.println("Element Found At Position :"+flag);
}
else
{
System.out.println("Element Not Found");
}
}
public static void main(String args[])
{
LinearSearch obj=new LinearSearch();
int search = 10;
int ar[]=new int[5];
obj.input(ar);
int index = obj.searching(search,ar);
obj.display(index);
}
}