Write a program that accepts 10 integer inputs from a user and store them in an array. The program must again ask the user to give another number and tell the user whether that number is present or not in the array. If the number is present, the program must identify its array location otherwise it should display a message “Number not Found!”.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] array = new int[10];
for (int i = 0; i < array.length; i++) {
System.out.print("Enter an integer: ");
array[i] = in.nextInt();
}
int index = -1;
System.out.print("A number to find: ");
int number = in.nextInt();
for (int i = 0; i < array.length; i++) {
if (array[i] == number) {
index = i;
break;
}
}
System.out.println(index == -1 ? "Number not Found!" : index);
}
}
Comments
Leave a comment