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:
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
Post a Comment