Sunday, July 16, 2017

Write a program that will ask the user to input three integer values from the keyboard. Then it will print the smallest and largest of those numbers using functions.

/**
 * Created by Jo on 7/10/2017.
 * For more exercises and lessons, visit http://shegertech.blogspot.com/
 */

 public class Q2Solution {
    void compare(int num1, int num2, int num3) {

        int smallest = 0, largest = 0;

        if(num1>num2 && num1>num3)
            largest = num1;
        else if (num2>num1 && num2>num3)
            largest= num2;
        else
            largest = num3;

        System.out.println("Largest number = " + largest);

        if(num1<num2 && num1<num3)
            smallest = num1;
        else if (num2<num1 && num2<num3)
            smallest = num2;
        else
            smallest =num3;

        System.out.println("Smallest number = " + smallest);

    }

    public static void main(String[] args) {
        int n1 = 9, n2 = 5, n3 = 4;

        Q2Solution q2 = new Q2Solution();
        q2.compare(n1, n2, n3);
    }
}

Share: