Answer to Question #283088 in Java | JSP | JSF for bens

Question #283088

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!”.

1
Expert's answer
2021-12-28T01:31:52-0500
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);
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog