The program below is the answer 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 |
/**<br /> *<br /> * @Author: Aghatise Osazuwa<br /> * Website: www.cscprogrammingtutorials.com<br /> *<br /> * Exercise 2.24 - Largest And Smallest Integers<br /> * This Program Determines The Largest And Smallest of Any Five Integer Numbers<br /> *<br /> */ <br /><br />import java.util.Scanner;<br /><br />public class Ex02_24 {<br /> public static void main (String [] args) {<br /><br /> Scanner value = new Scanner (System.in); <br /> <br /> int num1; <br /> int num2;<br /> int num3; <br /> int num4;<br /> int num5; <br /> int largest; <br /> int smallest;<br /> <br /> System.out.print ("nEnter Your First Number: ");<br /> num1= value.nextInt();<br /> System.out.print ("nEnter Your Second Number: ");<br /> num2= value.nextInt();<br /> System.out.print ("nEnter Your Third Number: ");<br /> num3= value.nextInt();<br /> System.out.print ("nEnter Your Fourth Number: ");<br /> num4= value.nextInt();<br /> System.out.print ("nEnter Your Fifth Number: ");<br /> num5= value.nextInt();<br /> <br /> largest = num1; // assume largest is the first number<br /> if (num2 > largest)<br /> largest = num2;<br /> if (num3 > largest)<br /> largest = num3;<br /> if (num4 > largest)<br /> largest = num4;<br /> if (num5 > largest)<br /> largest = num5;<br /> <br /> smallest = num1; // assume smallest is the first number<br /> if (num2 < smallest)<br /> smallest = num2;<br /> if (num3 < smallest)<br /> smallest = num3;<br /> if (num4 < smallest)<br /> smallest = num4;<br /> if (num5 < smallest)<br /> smallest = num5;<br /> <br /> System.out.printf ("nLargest = %dnSmallest = %dn", largest, smallest);<br /><br /> }<br />}<br /> |