Linear Search Program in C | C++ | Java | Python

Linear search program in c without using the function

Solution In C


Code:

#include<stdio.h>
int main()
{
    int size;
    printf("Enter size of the array:");
    scanf("%d",&size);
    int arr[size],i;
    printf("Enter the element of the array:");
    for(i=0;i<size;i++)
        scanf("%d",&arr[i]);
    printf("Enter the search element:");
    int search_elm;
    scanf("%d",&search_elm);
    int found=0;
    for(i=0;i<size;i++)
    {
        if(arr[i]==search_elm)
            found=1;
    }
    if(found==1)
        printf("Search element is found.");
    else
        printf("Search element is not found.");
}

Input/Output:
Enter size of the array:5
Enter the element of the array:10
12
7
8
9
Enter the search element:7
Search element is found.

Linear Search program in C plus plus

Solution In C++


Code:

#include<iostream>
using namespace std;
int main()
{
    int size;
    cout<<"Enter size of the array:";
    cin>>size;
    int arr[size],i;
    cout<<"Enter the element of the array:";
    for(i=0;i<size;i++)
        cin>>arr[i];
    cout<<"Enter the search element:";
    int search_elm;
    cin>>search_elm;
    int found=0;
    for(i=0;i<size;i++)
    {
        if(arr[i]==search_elm)
            found=1;
    }
    if(found==1)
        cout<<"Search element is found.";
    else
        cout<<"Search element is not found.";
}


Input/Output:
Enter size of the array:4
Enter the element of the array:10
12
13
16
Enter the search element:17
Search element is not found.

Linear search program in java using scanner class


Solution In Java

Code:

import java.util.Scanner;
public class Linear_Search {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int size;
    System.out.println("Enter size of the array:");
    size=cs.nextInt();
    int arr[]=new int[size],i;
    System.out.println("Enter the element of the array:");
    for(i=0;i<size;i++)
        arr[i]=cs.nextInt();
    System.out.println("Enter the search element:");
    int search_elm;
    search_elm=cs.nextInt();
    int found=0;
    for(i=0;i<size;i++)
    {
        if(arr[i]==search_elm)
            found=1;
    }
    if(found==1)
    System.out.println("Search element is found.");
    else
    System.out.println("Search element is not found.");
    cs.close();
}
}


Input/Output:
Enter size of the array:
5
Enter the element of the array:
6
8
9
10
11
Enter the search element:
10
Search element is found.

Linear search program in python using list

Solution In Python

arr=[]
size = int(input("Enter the size of the array: "))
print("Enter the Element of the array:")
for i in range(0,size):
    num = int(input())
    arr.append(num)

search_elm=int(input("Enter the search element: "))
found=0

for i in range(size):
    if arr[i]==search_elm:
            found=1

if found==1:
        print("Search element is found.")
else:
        print("Search element is not found.")

Input/Output:
Enter the size of the array: 5
Enter the Element of the array:
7
9
10
23
56
Enter the search element: 30
Search element is not found.

More:-

C/C++/Java/Python Practice Question 


Post a Comment

0 Comments