Answer:
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the first number: ");
int n1 = input.nextInt();
System.out.print("Enter the second number: ");
int n2 = input.nextInt();
System.out.print("Enter the third number: ");
int n3 = input.nextInt();
System.out.println("largest: " + LargestNumber(n1, n2, n3));
System.out.println("smallest: " + SmallestNumber(n1, n2, n3));
}
public static int LargestNumber(int num1, int num2, int num3){
int largestNum = 0;
if(num1>=num2 && num1>=num3)
largestNum = num1;
else if(num2>=num1 && num2>=num3)
largestNum = num2;
else if(num3>=num1 && num3>=num2)
largestNum = num3;
return largestNum;
}
public static int SmallestNumber(int num1, int num2, int num3){
int smallestNum = 0;
if(num1<=num2 && num1<=num3)
smallestNum = num1;
else if(num2<=num1 && num2<=num3)
smallestNum = num2;
else if(num3<=num1 && num3<=num2)
smallestNum = num3;
return smallestNum;
}
}
Explanation:
Create function called LargestNumber that takes three parameters, num1, num2, num3. Find the largest number using if else structure among them and return it. For example, if num1 is both greater than or equal to num2 and num3, then it is the largest one.
Create function called SmallestNumber that takes three parameters, num1, num2, num3. Find the smallest number using if else structure among them and return it. For example, if num1 is both smaller than or equal to num2 and num3, then it is the smallest one.
Inside the main, ask the user for the inputs. Call the functions and print the largest and smallest.