Thursday, May 25, 2017

program in java to search for a number in an unsorted array

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package datastrucarraya;
/**
 *
 * @author arpis
 */
import java.util.Scanner;
class a{
    public int al(int i){
         System.out.println("number found");
         int l =i+1;
        System.out.println("found at "+l);
       
         return i;
    }
}
public class searchunsortedarray{
    public static void main(String[] args) {
        int i = 0;
        boolean found= false;
        System.out.println("enter the limit of your array");
        Scanner sc = new Scanner(System.in);
      
         i = sc.nextInt();
        int a[] = new int[i];
        System.out.println("enter the elemets in your array");
        for (int j = 0; j < a.length; j++) {
            a[j] = sc.nextInt();
        }
 a sa= new a();
        System.out.println("enter the element you want to search for");
        int key;
        key = sc.nextInt();
        for (int j = 0; j < a.length; j++) {
                if(a[j]==key){
                    found= true;
                  sa.al(j);
                   
                }
                else{
                     found= false;
                }
        }
        if(found!=true){
         
            System.out.println("sorry! element not found");
           
           
           
        }
    }
}

No comments:

Post a Comment