Skip to content

Commit 04df86a

Browse files
SpreehaMadhavBahl
authored andcommitted
day 28 (#199)
1 parent b2410bd commit 04df86a

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

day28/Java/linearSearch1.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package javaapplication3;
7+
8+
/**
9+
* @date 28/01/19
10+
* @author SPREEHA DUTTA
11+
*/
12+
import java.util.*;
13+
public class linearSearch1 {
14+
public static void main(String []args)
15+
{
16+
Scanner sc=new Scanner(System.in);
17+
int n;int i;int p,c=-1;
18+
System.out.println("Enter size and array elements");
19+
n=sc.nextInt();
20+
int arr[]=new int[n];
21+
for(i=0;i<n;i++)
22+
arr[i]=sc.nextInt();
23+
System.out.println("Enter element to be found");
24+
p=sc.nextInt();
25+
for(i=0;i<n;i++)
26+
if(arr[i]==p)
27+
{
28+
c=i;
29+
break;
30+
}
31+
if(c!=-1)
32+
System.out.println(c);
33+
else
34+
System.out.println("undefined");
35+
}
36+
}

day28/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,40 @@ function linearSearch (arr, n) {
3737

3838
console.log (linearSearch ([1, 2, 3, 4, 5], 2));
3939
console.log (linearSearch ([1, 2, 3, 4, 5], 7));
40+
```
41+
42+
## Java Implementation
43+
44+
### [Solution](./Java/linearSearch1.java)
45+
46+
```java
47+
/**
48+
* @date 28/01/19
49+
* @author SPREEHA DUTTA
50+
*/
51+
import java.util.*;
52+
public class linearSearch1 {
53+
public static void main(String []args)
54+
{
55+
Scanner sc=new Scanner(System.in);
56+
int n;int i;int p,c=-1;
57+
System.out.println("Enter size and array elements");
58+
n=sc.nextInt();
59+
int arr[]=new int[n];
60+
for(i=0;i<n;i++)
61+
arr[i]=sc.nextInt();
62+
System.out.println("Enter element to be found");
63+
p=sc.nextInt();
64+
for(i=0;i<n;i++)
65+
if(arr[i]==p)
66+
{
67+
c=i;
68+
break;
69+
}
70+
if(c!=-1)
71+
System.out.println(c);
72+
else
73+
System.out.println("undefined");
74+
}
75+
}
4076
```

0 commit comments

Comments
 (0)