Program to Find an Element From an Array (Linear Search)

Program to Find an Element From an Array (Linear Search)

  • Write a program to Find an Element From an Array (Linear Search) in C
  • Write a program to Find an Element From an Array (Linear Search) in C++
  • Write a program to Find an Element From an Array (Linear Search) in Python
  • Write a program to Find an Element From an Array (Linear Search) in PHP
  • Write a program to Find an Element From an Array (Linear Search) in Java
  • Write a program to Find an Element From an Array (Linear Search) in Java Script
  • Write a program to Find an Element From an Array (Linear Search) in C#

Explanation:

Searching for an element in an array entails going over the array and comparing each element with the specified target value.

Steps:

  1. Input the Array and Target Value:
    • Take the array and the element to search (target value) as input.
  2. Iterate Through the Array:
    • Start from the first element and check if it matches the target value.
  3. Check for Match:
    • If a match is found, print the index (or position) of the element and exit the search.
    • If the loop completes without finding the target, indicate that the element is not present in the array.

Program to Find an Element From an Array (Linear Search)

#include <stdio.h>

int main() {
    int arr[] = {10, 20, 30, 40, 50};
    int size = sizeof(arr) / sizeof(arr[0]);
    int target, found = 0;

    printf("Enter the element to search: ");
    scanf("%d", &target);

    for (int i = 0; i < size; i++) {
        if (arr[i] == target) {
            printf("Element %d found at index %d\n", target, i);
            found = 1;
            break;
        }
    }

    if (!found) {
        printf("Element %d not found in the array.\n", target);
    }

    return 0;
}

#include <iostream>
using namespace std;

int main() {
    int arr[] = {10, 20, 30, 40, 50};
    int size = sizeof(arr) / sizeof(arr[0]);
    int target, found = 0;

    cout << "Enter the element to search: ";
    cin >> target;

    for (int i = 0; i < size; i++) {
        if (arr[i] == target) {
            cout << "Element " << target << " found at index " << i << endl;
            found = 1;
            break;
        }
    }

    if (!found) {
        cout << "Element " << target << " not found in the array." << endl;
    }

    return 0;

arr = [10, 20, 30, 40, 50]

target = int(input("Enter the element to search: "))

if target in arr:
    print(f"Element {target} found at index {arr.index(target)}")
else:
    print(f"Element {target} not found in the array.")

<?php
$arr = array(10, 20, 30, 40, 50);

echo "Enter the element to search: ";
$target = intval(trim(fgets(STDIN)));
$found = false;

foreach ($arr as $index => $value) {
    if ($value == $target) {
        echo "Element $target found at index $index\n";
        $found = true;
        break;
    }
}

if (!$found) {
    echo "Element $target not found in the array.\n";
}
?>

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int[] arr = {10, 20, 30, 40, 50};
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the element to search: ");
        int target = scanner.nextInt();
        boolean found = false;

        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == target) {
                System.out.println("Element " + target + " found at index " + i);
                found = true;
                break;
            }
        }

        if (!found) {
            System.out.println("Element " + target + " not found in the array.");
        }
    }
}

const arr = [10, 20, 30, 40, 50];

const prompt = require('prompt-sync')();
const target = parseInt(prompt("Enter the element to search: "));

const index = arr.indexOf(target);
if (index !== -1) {
    console.log(`Element ${target} found at index ${index}`);
} else {
    console.log(`Element ${target} not found in the array.`);
}

using System;

class Program {
    static void Main() {
        int[] arr = {10, 20, 30, 40, 50};
        
        Console.Write("Enter the element to search: ");
        int target = int.Parse(Console.ReadLine());
        bool found = false;

        for (int i = 0; i < arr.Length; i++) {
            if (arr[i] == target) {
                Console.WriteLine($"Element {target} found at index {i}");
                found = true;
                break;
            }
        }

        if (!found) {
            Console.WriteLine($"Element {target} not found in the array.");
        }
    }
}

List of All Programs