-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearchindex.java
More file actions
35 lines (35 loc) · 933 Bytes
/
Copy pathsearchindex.java
File metadata and controls
35 lines (35 loc) · 933 Bytes
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
package sorting;
import java.util.Scanner;
import java.util.Arrays;
public class searchindex {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int n,target;
System.out.println("enter the number of elements : ");
n=s.nextInt();
int ar[]=new int[n];
for(int i=0;i<n;i++)
{
System.out.println("enter the elements : "+i);
ar[i]=s.nextInt();
}
System.out.println("enter the targeted elements : ");
target=s.nextInt();
for(int i=0;i<n-1;i++)
{
if(ar[i]==target)
{
System.out.println(i);
break;
}
}
for(int i=n-1;i>0;i--)
{
if(ar[i]==target)
{
System.out.println(i);
break;
}
}
}
}