Sunday, 13 October 2013

PROGRAM TO SEARCH AN ELEMENT IN A GIVEN ARRAY

//PROGRAM TO SEARCH AN ELEMENT IN A GIVEN ARRAY
//Developed by Ahlam Ansari
class SearchArray
{
    public static void main(String ahlam[ ])
    {
        int a[ ]={3,6,8,9},d,f=0,i,c=0;
        d=10;
        System.out.println("Elements in the array are: 3,6,8,9");
        System.out.println("Element to be search is 10");
        for(i=0;i<=3;i++)
        {   
            if(d==a[i])
            {
                f=1;
                c=i+1;
            }   
        }
        if(f==1)
        {
            System.out.println("Element is present at location:"+c);
        }
        else
        {
            System.out.println("Element is not present in the array");
        }
    }
}

/*
C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>javac SearchArray.jav
a

C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>java SearchArray
Elements in the array are: 3,6,8,9
Element to be search is 9
Element is present at location:4

C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>java SearchArray
Elements in the array are: 3,6,8,9
Element to be search is 10
Element is not present in the array

C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>
*/

No comments:

Post a Comment