Tuesday, 10 November 2020

Java Program on Linear Search

 //Linear Search in an array of size 10

import java.util.*;

public class Linear_Search

{

    public static void main(String args[])

    {

        Scanner in=new Scanner(System.in);

        int m[]=new int[10];

        int search,i,k=0;

        System.out.println("Enter 10 elements");

        for(i=0;i<10;i++)

        m[i]=in.nextInt();

        System.out.println("Enter the Search Element");

        search=in.nextInt();

        for(i=0;i<10;i++)

        {

            if(m[i]==search)

            {

            k=1;

            break;

            }

        }    

        if(k==1)

        System.out.println(search+" is present in the array");

        else 

        System.out.println(search+" is not present in the array");

    }

}

No comments:

Post a Comment

Java Program to count the total number of characters in a string

 //Java Program to count the total number of characters in a string import java.util.*; public class CountChars {     public static void mai...