Interview Question from PrepBytes with Answers.
- Badu Blogger

- Jul 14, 2019
- 1 min read

Answer is ::
Program ::
Using BufferedReader Class
import java.io.*; //Input-Output package imported
public class PrepBytes1
{
public static void main(String args[]) throws IOException // throws is a
//keyword in Java which is used in the signature of method to indicate
//that this method might throw one of the listed type exceptions .
{
int i,flag;
char ch;
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)); //BufferedReader reads from an
//InputStreamReader( reads bytes from System.in ) that reads from
//System.in( is an InputStream ).
System.out.println("Enter the Size of an Array ::");
int n = Integer.parseInt(buf.readLine());
int A[] = new int[n];
System.out.println("Enter Sorted Array Elements ::");
for(i=0;i<n;i++)
{
A[i]=Integer.parseInt(buf.readLine()); //parseInt method is used to
//convert the String value taken from keyboard to Integer.
}
do
{
System.out.println("Enter the Element to be Searched ::");
int key = Integer.parseInt(buf.readLine());
flag=0;
for(i=0;i<n;i++)
{
if(key==A[i])
{
flag=1;
break;
}
}
if(flag == 1)
{
System.out.println("Element Found");
}
else
{
System.out.println("Element Not Found");
}
System.out.println("Want to Continue (y/n)");
ch = buf.readLine().charAt(0);
}while(ch=='y'||ch=='Y');
}
}
Output ::
Enter the Size of an Array ::
5
Enter Sorted Array Elements ::
1
2
3
4
5
Enter the Element to be Searched ::
2
Element Found
Want to Continue (y/n)
y
Enter the Element to be Searched ::
9
Element Not Found
Want to Continue (y/n)
n
Hope u Understood the concept. Happy Learning.

Comments