Write a program using one-dimensional array that determines the highest value among the five input values from the keyboard and prints the difference of each value from the highest.
enter five numbers: 10 20 15 7 8
the highest is: 20
the difference from the highest are:
20 – 10 = 10
20 – 20 = 0
20 – 15 = 5
20 – 7 = 13
20 – 8 = 12
import java.util.Scanner;
public class App {
/**
* The start point of the program
*
* @param args
*
*/
public static void main(String[] args) {
Scanner keyBoard = new Scanner(System.in);
int numbers[] = new int[5];
System.out.print("enter five numbers: ");
for (int i = 0; i < numbers.length; i++) {
numbers[i] = keyBoard.nextInt();
}
int highest = numbers[0];
for (int i = 1; i < numbers.length; i++) {
if (highest < numbers[i]) {
highest = numbers[i];
}
}
System.out.println("the highest is: " + highest);
System.out.println("the difference from the highest are:");
for (int i = 0; i < numbers.length; i++) {
System.out.printf("%d – %d = %d\n", highest, numbers[i], (highest - numbers[i]));
}
keyBoard.close();
}
}
Comments
Leave a comment