Find Highest & Lowest of Five Integers Using Java Loops code example

Example: find highest and lowest of five integers using java loops

//Declare max as Minimum and min as Maximum

int max = Integer.MIN_VALUE, min = Integer.MAX_VALUE;

//Declare Scanner to take input from user

Scanner sc = new Scanner(System.in);

System.out.print("Enter 5 integers: ");

//for loop that runs 5 times to take input from user

for (int i = 0; i < 5; ++i) {

int num = sc.nextInt();

//Check if the number is greater than max

if (max < num)

max = num;

//Check if the number is smaller than min

if (min > num)

min = num;

}

//Print the Highest number which is stored in max

System.out.println("Highest integer is " + max);
//Print the Lowest number which is stored in min

System.out.println("Lowest integer is " + min);

Tags:

Java Example