The program below is the solution to Deitel’s Java How to Program (9th Edition) Chapter 2 Exercise 2.24.
Question: Write an application that reads five integers and determines and prints the largest and smallest integers in the group. Use only the programming techniques you learned in this chapter.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
/** * * Exercise 2.24 - Largest And Smallest Integers * This Program Determines The Largest And Smallest of Any Five Integer Numbers * */ import java.util.Scanner; public class Ex02_24 { public static void main (String [] args) { Scanner value = new Scanner (System.in); int num1; int num2; int num3; int num4; int num5; int largest; int smallest; System.out.print ("\nEnter Your First Number: "); num1= value.nextInt(); System.out.print ("\nEnter Your Second Number: "); num2= value.nextInt(); System.out.print ("\nEnter Your Third Number: "); num3= value.nextInt(); System.out.print ("\nEnter Your Fourth Number: "); num4= value.nextInt(); System.out.print ("\nEnter Your Fifth Number: "); num5= value.nextInt(); largest = num1; // assume largest is the first number if (num2 > largest) largest = num2; if (num3 > largest) largest = num3; if (num4 > largest) largest = num4; if (num5 > largest) largest = num5; smallest = num1; // assume smallest is the first number if (num2 < smallest) smallest = num2; if (num3 < smallest) smallest = num3; if (num4 < smallest) smallest = num4; if (num5 < smallest) smallest = num5; System.out.printf ("\nLargest = %d\nSmallest = %d\n", largest, smallest); } } |