Linear Search ■ program to check an element is present in array or not using boolean datatype in java.
■》Code link-> click ππππ
https://www.programiz.com/online-compiler/5fkg6PNTiyxLS
■》 Program ->
import java.util.*;
public class Ved
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("enter size=");
int n =sc.nextInt();
int [] arr=new int[n];
System.out.print("enter elements of array=");
for(int i=0; i<n; i++){
arr[i]=sc.nextInt();
}
System.out.print("enter element=");
int x=sc.nextInt();
boolean flag =false;
for(int i=0; i<n; i++){
if(arr[i]==x){
flag=true;
break;
}
}
if(flag==true) System.out.println("element found");
else System.out.println(" element not found");
}
}
■》 output - >
enter size=4
enter elements of array=
12
13
45
56
enter element=13
element found
Comments
Post a Comment