java program for searching an element in the array and displaying index number as well

package arraysearch;
import java.io.*;
import java.util.*;

public class Arraysearch {

   
    public static void main(String[] args) {
   
        int num,i,num2,flag=0;
                System.out.println(“Enter the size of array”);
                Scanner sc=new Scanner(System.in);
                num=sc.nextInt();
                System.out.println(“Enter the element in the array one by one”);
                int array[]=new int[50];
                for(i=0;i<num;i++)
                {
                    array[i]=sc.nextInt();
                }
                System.out.println(“The number entered in the arrray is”);
                for(i=0;i<num;i++)
                {
                    System.out.println(array[i]);
                }
                System.out.println(“Enter the element to search”);
                num2=sc.nextInt();
                for(i=0;i<num;i++){
                 
               
                if(num2==array[i]){
                flag++;  
                }
                }
                if(flag==1)
                System.out.println(“found at index “+i);
                else
               
                System.out.println(“not found”);
               
    }
   
}
OUTPUT-
Enter the size of array
7
Enter the element in the array one by one
45
89
12
32
64
97
19
The number entered in the arrray is
45
89
12
32
64
97
19
Enter the element to search
89
found at index 7

Leave a Comment

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

To Resolve this issue at just 5$ from ServerExpert

https://serverexpert.io/

For Hosting please visit Hostingwebsite.io

You have Successfully Subscribed!

Open chat