Find the index of any number in array in java language.

import java.util.*;
public class array {
    public static void main(String[] args) {
        int size;
        Scanner sc=new Scanner(System.in);
        System.out.println("enter the size of array=");
         size =sc.nextInt();
        int number[]=new int[size];
        System.out.println("enter the numbers=");
        for(int i=0; i<size; i++){
            number[i]=sc.nextInt();
        }
        System.out.println("enter a number for search=");
        int x=sc.nextInt();
        for(int i=0; i<number.length ; i++ ){
            if(number[i]==x){
                System.out.print("the index of given number is="+i);
                break;
            }
        }
    }
}

■》Output->

enter the size of array=4
enter the numbers=
1
4
5
8
enter a number for search= 5
the index of given number is=2

Comments

Popular posts from this blog

Introduction of java Programming language.

Stack data structure.