🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
Linear search is a simple way to find a specific number in a list. It starts at the beginning of the list and checks each number one by one. If it finds the number you're looking for, it stops. Otherwise, it keeps going until it reaches the end of the list. In this blog post, we'll show you how to do this with a C++ program.
2. Program Overview
1. Define and initialize the array or list.
2. Accept the number to be searched.
3. Scan each element of the list.
4. If the element matches with the searched number, display its position.
5. If the end of the list is reached, indicate that the number is not present.
3. Code Program
#include <iostream>
using namespace std;
int main() {
int n, num, i, flag = 0;
// Input number of elements
cout << "Enter the number of elements in the array: ";
cin >> n;
int arr[n];
// Input array elements
cout << "Enter the elements of the array:" << endl;
for(i = 0; i < n; i++) {
cin >> arr[i];
}
// Number to be searched
cout << "Enter the number to be searched: ";
cin >> num;
// Linear Search
for(i = 0; i < n; i++) {
if(arr[i] == num) {
flag = 1;
break;
}
}
if(flag == 1) {
cout << "Number " << num << " is present at position " << i+1 << endl;
} else {
cout << "Number " << num << " is not present in the array." << endl;
}
return 0;
}
Output:
Enter the number of elements in the array: 5 Enter the elements of the array: 14 25 36 32 26 Enter the number to be searched: 32 Number 32 is present at position 4
4. Step By Step Explanation
1. Array Initialization: We capture the size of the array and its elements from the user.
2. Number to be Searched: We also capture the number that needs to be searched within the list.
3. Linear Search: We then go through each element of the list one by one. If the element matches with the number we are searching for, we set a flag and break out of the loop.
4. Result: Depending on the flag value, we either display the position where the number is found or mention that the number is not present in the list.
Linear search is straightforward but not the most efficient searching algorithm, especially for large lists. However, it serves as a fundamental introduction to searching techniques in computer science.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment