import java.io.BufferedReader;
import java.io.InputStreamReader;
/**
* Write a program to declare an integer array.
* accept a number which is to searched in the array.
* display the position at which the number is found within the array.
*/
public class Searcharray
{
/**
* gets the integer input from from user
* and search the position on that integer and display it
* @throws Exception
*/
public static void getIntgerNsearchpos() throws Exception
{
// source array
final int[] intSourceArray={0,1,2,3,4,5,6,7,8,9,10};
// get the search integer from user..
BufferedReader brInput=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter any integer to search : ");
int intSearchNo=Integer.parseInt(brInput.readLine());
/// find position of no in array
for(int i=0;i<intSourceArray.length;i++)
{
if(intSourceArray[i]==intSearchNo)
{
System.out.println("Position of " + intSearchNo + " in source array is " + i);
return;
}
}
System.out.println(intSearchNo + " not exists in source array");
}
/**
* excecution begin from here
* @param args
*/
public static void main(String[] args)
{
try
{
getIntgerNsearchpos();
}
catch(Exception e)
{
System.out.println("Error occured!!");
}
}
}
No comments:
Post a Comment