Answer to Question #337659 in Java | JSP | JSF for mjayyyyyyyyyyyyyyy

Question #337659

Create a program using one-dimensional array that searches a number if it is found on the list of the given 5 input numbers and locate it exact location in the list


1
Expert's answer
2022-05-05T14:07:03-0400
import java.util.Scanner;

public class Answer {
    static int find(int[] arr , int el) {
        for (int i = 0; i < arr.length; i++) {
            if (el == arr[i]) {
                return i;
            }
        }
        return -1;
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[] array = new int[5];
        System.out.println("Enter 5 elements of array: ");
        for (int i = 0; i < array.length; i++) {
            array[i]  = scanner.nextInt();
        }
        System.out.println("Enter element to find: ");
        int el = scanner.nextInt();
        int ans = find(array, el);
        if (ans == -1) {
            System.out.println("Element not found");
        } else {
            System.out.println("Location: " + ans);
        }
    }
}

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
APPROVED BY CLIENTS