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?
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();
}
}
Comments
Leave a comment