Monday, June 29, 2015

// // Leave a Comment

Java Program To Find Largest And Smallest Number

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.

/**
 *
 * @Author: Aghatise Osazuwa
 * Website: www.cscprogrammingtutorials.com
 */

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);

    }
}

0 comments:

Post a Comment


DISCLAIMER: Opinions expressed in comments are those of the comment writers alone and does not reflect or represent the views of the post author. We reserve the right to delete any post deemed inappropriate or offensive and/or spammy. Please do not use abusive words/hate speech.

I Appreciate your valuable Feedback. So, Please DO NOT SPAM - Spam comments will be deleted immediately.

Don't use brand name in name field and you're not allowed to use links in comments unless it's necessary. Such comments will be removed immediately.

Thanks.