Write a program to accept N values in an array and search for an value accepted from user (along with its position).
Answers
Answered by
0
import java.util.*;
class Search
{
public static void main()
{
int i,N,srch,pos=0;
boolean flag=false;
Scanner sc=new Scanner (System.in);
System.out.println("Enter your N values");
N=sc.nextInt();
int m[]=new int[N];
for(i=0;i<m.length;i++)
{
System.out.println("Enter the elements in array");
m[i]=sc.nextInt();
}
System.out.println("Enter your searched element ");
srch=sc.nextInt();
for(i=0;i<m.length;i++)
{
if(srch==m[i])
{
flag=true;
pos=i+1;
break;
}
}
if(flag==true)
System.out.println("Searched element is found at "+ pos);
else
System.out.println("Element not found");
}
}
Hope it will help you @
Mark me brainlest @
Similar questions