Monday, 18 February 2013


Program to demonstrate Linear Search Using Arrays

Code:
/**
 * Program to demonstrate Linear Search Using Arrays.
 **/
import java.io.*;
public class Arry_Search
{
     public static void main()throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       System.out.print("Enter the number of elements you want to input ==> ");
       int size = Integer.parseInt(br.readLine());
        int arry[] = new int[size];
        int flag = 0, loc = 0;
        for(int i=0;i<arry.length;i++)
         {
             System.out.print("Enter number "+(i+1)+" ==> ");
             arry[i] = Integer.parseInt(br.readLine());
         }
        System.out.print("Enter number to be searched ==> ");
        int num = Integer.parseInt(br.readLine());
        for(int i = 0; i < arry.length ;i++ )
        {
            if (arry[i] == num)
            {
                flag = 1;
                loc = i;
                break;
            }
        }
        if (flag == 1)
        {
            System.out.println("Number "+num+" found in "+(loc+1)+" location");
        }
        else
        {
            System.out.println("Number not found ");
        }
    }
}

OUTPUT
Enter the number of elements you want to input ==> 4
Enter number 1 ==> 10
Enter number 2 ==> 20
Enter number 3 ==> 30
Enter number 4 ==> 40
Enter number to be searched ==> 30
Number 30 found in 3 location

Related Posts :



1 comment:

  1. Can you show mw the solution for this program
    to input name and temperature of 10 cities and print the city which has maximum temperatue

    ReplyDelete