Answer to Question #324293 in Java | JSP | JSF for Angge

Question #324293

Write a program to input 10 numbers and output the sum of the two largest values in the sequence.


Sample Output:


Enter the Numbers >> 10 2 3 4 5 6 7 9 9 10


Sum >> 20




Try Again Y/N?

1
Expert's answer
2022-04-05T17:28:54-0400


import java.util.Scanner;


public class App {


	static int sumTwoLargesеtValues(int[] numbers) {
		int temp;
		for (int i = 0; i < numbers.length; i++) {
			for (int j = i + 1; j < numbers.length; j++) {
				if (numbers[i] > numbers[j]) {
					temp = numbers[i];
					numbers[i] = numbers[j];
					numbers[j] = temp;
				}
			}
		}
		return numbers[numbers.length - 2] + numbers[numbers.length - 1];
	}


	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		String answer = "";
		while (answer.compareToIgnoreCase("n") != 0) {
			int[] numbers = new int[10];
			System.out.print("Enter the Numbers >>  ");
			for (int i = 0; i < 10; i++) {
				numbers[i] = in.nextInt();
			}
			System.out.println("Sum >> " + sumTwoLargesеtValues(numbers));
			in.nextLine();
			System.out.print("Try Again Y/N? ");
			answer = in.nextLine();
		}


		in.close();
	}


}

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