Java program to search an entered element in array

Search an entered element in array

This program shows the concept to search an entered element in array.The method used in the program is simple search method i.e. comparing each element.

import java.util.*;
class scan
{
public static void main(String arg[])
 {
  int[] x=new int[100];
  int i,y,z,g=0;
  
   Scanner kb=new Scanner(System.in);
  System.out.println("Enter Size of array:-");
   y=kb.nextInt();
   System.out.println("Enter array");
    for(i=0;i<y;i++)
	 {
	  x[i]=kb.nextInt();
	 }
	System.out.println("Enter Number you want to search:-");
    z=kb.nextInt();	
	
	for(i=0;i<y;i++)
	{
		if(z==x[i])
		{ 
	        g=1;
          System.out.println("Element is at position:- "+(i+1));
		}
		
	}
	if(g==0)
		{
			System.out.println("Entered Element not found");
		}
	
}
}

OUTPUT:-

search an entered element in array


Comment below if you are facing any problem.


Also read

Java Program to enter Elements of your choice in Array
Java Program to find greatest Element in array


Summary
Java program to search an entered element in array
Article Name
Java program to search an entered element in array
Description
This program shows the concept to search an entered element in array.The method used in the program is simple search method i.e. comparing each element.
Author
Publisher Name
Scanfcode
Publisher Logo

Leave a Reply

Your email address will not be published. Required fields are marked *