Answer to Question #285095 in Java | JSP | JSF for angge

Question #285095

We've already tried comparing 3 numbers to see the largest among all, so let's try a more complicated mission where we locate the position of the largest among 5 numbers. There are only 4 possible cases for this mission:

- if the largest digit is the first digit, print "Leftmost"

- if the largest digit is the third digit, print "Middle"

- if the largest digit is the last digit, print "Rightmost"

- if none of the above is correct, print "Unknown"



1
Expert's answer
2022-01-06T02:01:18-0500
public class Main {
    public static void main(String[] args) {
        int[] numbers = {1, 51, 25, 6, 66};
        int largest = 0;
        for (int i = 0; i < numbers.length; i++) {
            if (numbers[i] > numbers[largest]) {
                largest = i;
            }
        }
        if (largest == 0) {
            System.out.println("Leftmost");
        } else if (largest == numbers.length / 2) {
            System.out.println("Middle");
        } else if (largest == numbers.length - 1) {
            System.out.println("Rightmost");
        } else {
            System.out.println("Unknown");
        }
    }
}

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