find highest and lowest of five integers using java loops

Solutions on MaxInterview for find highest and lowest of five integers using java loops by the best coders in the world

showing results for - "find highest and lowest of five integers using java loops"
Paola
16 Nov 2018
1//Declare max as Minimum and min as Maximum
2
3int max = Integer.MIN_VALUE, min = Integer.MAX_VALUE;
4
5//Declare Scanner to take input from user
6
7Scanner sc = new Scanner(System.in);
8
9System.out.print("Enter 5 integers: ");
10
11//for loop that runs 5 times to take input from user
12
13for (int i = 0; i < 5; ++i) {
14
15int num = sc.nextInt();
16
17//Check if the number is greater than max
18
19if (max < num)
20
21max = num;
22
23//Check if the number is smaller than min
24
25if (min > num)
26
27min = num;
28
29}
30
31//Print the Highest number which is stored in max
32
33System.out.println("Highest integer is " + max);
34//Print the Lowest number which is stored in min
35
36System.out.println("Lowest integer is " + min);