-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathCheckForKey.java
More file actions
51 lines (42 loc) · 1.42 KB
/
CheckForKey.java
File metadata and controls
51 lines (42 loc) · 1.42 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
package Recursion;
import java.util.Scanner;
public class CheckForKey {
public static int Fkey(int arr[],int key,int i){
if(i==arr.length){
System.out.println("Key not found ");
return -1;
}
if(arr[i]==key){
System.out.println("Key Found at index :"+ i);
return 1;
}
return Fkey(arr, key, i+1);
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Size of Array ");
int x=sc.nextInt();
int n[]=new int[x];
System.out.println();
System.out.println("Input the values in Array ");
for(int i=0;i<n.length;i++){
n[i]=sc.nextInt();
}
System.out.println();
System.out.println("do you want to find a key of a value ");
System.out.print("Enter 1 for Yes "+" / ");
System.out.println("Enter 0 for No.");
int inp=sc.nextInt();
if(inp==1){
System.out.println("Enter the number you want to find ");
int y=sc.nextInt();
System.out.print(Fkey(n, y, 0));
}
else if(inp==0){
System.out.println("OK");
}
else if(inp!=1 || inp!=0){
System.out.println("Enter a valid input");
}
}
}