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

Program to illustrate the concept of templates.

Example of Synopsis in LaTeX

To create an html file to implement the concept of margin, padding using cascading style sheets.