//Program to search an element in an array
//Program by Ahlam Ansari
import java.util.*;
class Search
{
public static void main(String [] ahlam)
{
// int arr[]={1,2,3,4,5,6,7,8,9};
int lim=9;
int arr1[]=new int[lim];
System.out.println("Enter the arr values");
Scanner scan=new Scanner(System.in);
for(int i=0;i<arr1.length;i++)
{
arr1[i]=scan.nextInt();
}
System.out.println("Enter the arr values to be searched");
int num=scan.nextInt();
boolean flag=false;
for(int i=0;i<arr1.length;i++)
{
if(arr1[i]==num)
{
System.out.println("Value Exist at loc "+i);
flag=true;
}
}
if(flag==false)
{
System.out.println("Element was not found");
}
}
}
/*C:\Documents and Settings\AHLAM\My Documents\Google Drive\My Lectures\Fall\OOPM\
Programs>javac Search.java
C:\Documents and Settings\AHLAM\My Documents\Google Drive\My Lectures\Fall\OOPM\
Programs>java Search
Enter the arr values
5
1
2
3
4
5
9
3
5
Enter the arr values to be searched
9
Value Exist at loc 6
C:\Documents and Settings\AHLAM\My Documents\Google Drive\My Lectures\Fall\OOPM\
Programs>
*/
//Program by Ahlam Ansari
import java.util.*;
class Search
{
public static void main(String [] ahlam)
{
// int arr[]={1,2,3,4,5,6,7,8,9};
int lim=9;
int arr1[]=new int[lim];
System.out.println("Enter the arr values");
Scanner scan=new Scanner(System.in);
for(int i=0;i<arr1.length;i++)
{
arr1[i]=scan.nextInt();
}
System.out.println("Enter the arr values to be searched");
int num=scan.nextInt();
boolean flag=false;
for(int i=0;i<arr1.length;i++)
{
if(arr1[i]==num)
{
System.out.println("Value Exist at loc "+i);
flag=true;
}
}
if(flag==false)
{
System.out.println("Element was not found");
}
}
}
/*C:\Documents and Settings\AHLAM\My Documents\Google Drive\My Lectures\Fall\OOPM\
Programs>javac Search.java
C:\Documents and Settings\AHLAM\My Documents\Google Drive\My Lectures\Fall\OOPM\
Programs>java Search
Enter the arr values
5
1
2
3
4
5
9
3
5
Enter the arr values to be searched
9
Value Exist at loc 6
C:\Documents and Settings\AHLAM\My Documents\Google Drive\My Lectures\Fall\OOPM\
Programs>
*/
No comments:
Post a Comment