EduLearn - Online Education Platform

Welcome to EduLearn!

Start learning today with our wide range of courses taught by industry experts. Gain new skills, advance your career, or explore new interests.

Browse Courses

Here is a simple Java program to perform Linear Search on an array:

 

βœ… Linear Search Example in

import java.util.Scanner;

 

public class LinearSearch {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

 

// Input array size

System.out.print(“Enter the number of elements: “);

int n = sc.nextInt();

 

int[] arr = new int[n];

 

// Input array elements

System.out.println(“Enter the elements:”);

for (int i = 0; i < n; i++) {

arr[i] = sc.nextInt();

}

 

// Input the element to search

System.out.print(“Enter the element to search: “);

int key = sc.nextInt();

 

// Linear search

boolean found = false;

for (int i = 0; i < n; i++) {

if (arr[i] == key) {

System.out.println(“Element found at index ” + i);

found = true;

break;

}

}

 

if (!found) {

System.out.println(“Element not found in the array.”);

}

 

sc.close();

🟒 Sample Output

Enter the number of elements: 5

Enter the elements:

10

20

30

40

50

Enter the element to search: 30

Element found at index 2

 

}

}

 

Linear search in java program

Leave a Reply

Your email address will not be published. Required fields are marked *

EduLearn - Online Education Platform

Welcome to EduLearn!

Start learning today with our wide range of courses taught by industry experts. Gain new skills, advance your career, or explore new interests.

Browse Courses

Popular Courses

[Course Image]

Introduction to Programming

Learn the fundamentals of programming with Python in this beginner-friendly course.

12 Hours Beginner
[Course Image]

Data Science Essentials

Master the basics of data analysis, visualization, and machine learning.

20 Hours Intermediate
[Course Image]

Web Development Bootcamp

Build modern websites with HTML, CSS, JavaScript and popular frameworks.

30 Hours Beginner
[Course Image]

Digital Marketing Fundamentals

Learn SEO, social media marketing, email campaigns and analytics.

15 Hours Beginner
Educational Website Footer