Write a program to find the location of a given element using Linear Search in Java

import java.util.Scanner;
class DSPM3
{
public static void main(String args[])
{
Scanner mi = new Scanner(System.in);

int arr[]=new int[100];
int i,count=0;
System.out.println("Enter the no. of elements to be enter:");
int n =mi.nextInt();
System.out.println("Enter the elements:");
for(i=0;i<n;i++)
{
arr[i]=mi.nextInt();
}
System.out.println("\nEnter the element to be Searched:");
       int e=mi.nextInt();
       for(i=0;i<n;i++)
{
       if(arr[i]==e)
       count++;
}
       if(count==0)
       System.out.println("\nElement not found");
       else if(count==1)
       System.out.println("\nElement "+e+" exist " + count+" time in Array");
       else
       System.out.println("\nElement "+e+" exist " + count+" times in Array");


System.out.println("/*\nName-Sanjampreet Singh\nRoll Number-1507967\n*/");
}
}
OUTPUT:

Comments

Popular posts from this blog

To create an html page with frames and frameset

1. Write a program to insert a new element at end as well as at a given position in an array in Java

1. To familiarize with network devices like switch, hub, routers and bridges in LaTex