Java Program to Perform a Linear Search

Write a Java program to perform a linear search on arrays. In this Java linear search example, the for loop traverses the array items from start to end.

The if statement checks each number against the search item. If it finds the match, printing the index position and break statement helps the javac exit from the loop.

package Remaining;

import java.util.Scanner;

public class LinearSearch0 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		int i;
		
		sc = new Scanner(System.in);		
		
		int[] arr = {10, 20, 40, 60, 80, 100, 120, 150};
		
		System.out.print("Please Enter Number to Search =  ");
		int num = sc.nextInt();
		
		for(i = 0; i < arr.length; i++) 
		{
			if(arr[i] == num) 
			{
				System.out.println("The Index Position of " + num + " = " + i);
				break;
			}
		}
		if(i == arr.length)
		{
			System.out.println("Number Not Found");
		}
	}
}
Java Program to Perform a Linear Search

This Java example accepts the user input array size, items and performs the linear search to find the number.

package Remaining;

import java.util.Scanner;

public class LinearSearch1 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		int Size, i;
		
		sc = new Scanner(System.in);		
		System.out.print("Please Enter the Array size = ");
		Size = sc.nextInt();
		
		int[] arr = new int[Size];
		
		System.out.format("Enter Array %d elements = ", Size);
		for(i = 0; i < Size; i++) {
			arr[i] = sc.nextInt();
		}
		
		System.out.print("Please Enter Number to Search =  ");
		int num = sc.nextInt();
		
		for(i = 0; i < arr.length; i++) 
		{
			if(arr[i] == num) 
			{
				System.out.println("The Index Position of " + num + " = " + i);
				break;
			}
		}
		if(i == arr.length)
		{
			System.out.println("Number Not Found");
		}
	}
}
Please Enter the Array size = 8
Enter Array 8 elements = 11 23 45 67 77 89 98 100
Please Enter Number to Search =  98
The Index Position of 98 = 6

Java program to perform a linear search on arrays using functions. 

package Remaining;

import java.util.Scanner;

public class LinearSearch2 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		
		sc = new Scanner(System.in);		
		System.out.print("Please Enter the Array size = ");
		int Size = sc.nextInt();
		
		int[] arr = new int[Size];
		
		System.out.format("Enter Array %d elements = ", Size);
		for(int i = 0; i < Size; i++) {
			arr[i] = sc.nextInt();
		}
		
		System.out.print("Please Enter Number to Search =  ");
		int num = sc.nextInt();
		
		int result = linearSearch(arr, num);
		if(result == -1)
		{
			System.out.println("Number Not Found");
		}
		else
		{
			System.out.println("The Index Position of " + num + " = " + result);

		}
	}
	
	public static int linearSearch(int arr[], int num)
	{
		for(int i = 0; i < arr.length; i++) 
		{
			if(arr[i] == num) 
			{
				return i;
			}
		}
		return -1;
	}
}
Please Enter the Array size = 6
Enter Array 6 elements = 22 30 45 80 90 120
Please Enter Number to Search =  90
The Index Position of 90 = 4