Java program for Linear search



Programs

package com.candidjava;

import java.util.Scanner;

class LinearSearch
{
	public static void main(String args[])
	{
		int i, num, searchval, array[];

		Scanner in = new Scanner(System.in);
		System.out.println("Enter number of elements");
		num = in.nextInt();

		array = new int[num];
		System.out.println("Enter " + num + " integers");

		for (i = 0; i < num; i++)
			array[i] = in.nextInt();

		System.out.println("Enter the search value:");
		searchval = in.nextInt();
		
		in.close();
		for (i = 0; i < num; i++)
		{
			if (array[i] == searchval)
			{
				System.out.println(searchval + " is present at location " + (i + 1));
				break;
			}
		}
		if (i == num)
			System.out.println(searchval + " is not exist in array.");
	}
}


Output

Enter number of elements

6

Enter 6 integers

45

32

28

50

65

39

Enter the search value:

50

50 is present at location 4




Related Post

Comments


©candidjava.com