Code :
package demo;
import java.util.Scanner;
/**
*
* @author www.codetheta.com
*/
public class LinearSearch {
public static void main(String[] args) {
int array[], elements, elem_search, each_elements, i, flag = 0;
Scanner in = new Scanner(System.in);
System.out.println("Enter number of array elements: ");
elements = in.nextInt();
array = new int[elements];
System.out.println("Enter array elements: ");
for (i=0; i<elements; i++)
array[i] = in.nextInt();
System.out.println ("Enter element to find: ");
elem_search = in.nextInt();
for (i=0; i<elements; i++) {
if (array[i] == elem_search) {
flag = 1;
break;
}
}
if (flag == 0) {
System.out.println("Not Found");
}
else {
System.out.println("Found");
}
}
}
Output :
Enter number of array elements:
5
Enter array elements:
4
7
6
2
8
Enter element to find:
8
Found
IDE Used To Test This Code : NetBeans IDE 8.2
Try this code in your computer for better understanding. Enjoy the code. If you have any Question you can contact us or mail us.We will reply you as soon as possible.
package demo;
import java.util.Scanner;
/**
*
* @author www.codetheta.com
*/
public class LinearSearch {
public static void main(String[] args) {
int array[], elements, elem_search, each_elements, i, flag = 0;
Scanner in = new Scanner(System.in);
System.out.println("Enter number of array elements: ");
elements = in.nextInt();
array = new int[elements];
System.out.println("Enter array elements: ");
for (i=0; i<elements; i++)
array[i] = in.nextInt();
System.out.println ("Enter element to find: ");
elem_search = in.nextInt();
for (i=0; i<elements; i++) {
if (array[i] == elem_search) {
flag = 1;
break;
}
}
if (flag == 0) {
System.out.println("Not Found");
}
else {
System.out.println("Found");
}
}
}
Enter number of array elements:
5
Enter array elements:
4
7
6
2
8
Enter element to find:
8
Found
IDE Used To Test This Code : NetBeans IDE 8.2
Try this code in your computer for better understanding. Enjoy the code. If you have any Question you can contact us or mail us.We will reply you as soon as possible.
Post a Comment