Answer:
The java program is as follows.
import java.util.Scanner;
import java.lang.*;
public class Numbers
{
//variables to hold three integers
static int num1, num2, num3;
//method to return largest of all three numbers
public static int largestNumber(int num1, int num2, int num3)
{
if(num1>num2 && num1>num3)
return num1;
else if(num2>num1 && num2>num3)
return num2;
else
return num3;
}
//method to return smallest of all three numbers
public static int smallestNumber(int num1, int num2, int num3)
{
if(num1<num2 && num1<num3)
return num1;
else if(num2<num1 && num2<num3)
return num2;
else
return num3;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter three numbers: ");
num1= sc.nextInt();
num2= sc.nextInt();
num3= sc.nextInt();
int largest = largestNumber(num1, num2, num3);
int smallest = smallestNumber(num1, num2, num3);
System.out.println("Largest number is "+ largest);
System.out.println("Smallest number is "+ smallest);
}
}
OUTPUT
Enter three numbers: 12 13 15
Largest number is 15
Smallest number is 12
Explanation:
1. Three integer variables are declared to hold the three user inputted numbers.
2. The variables are declared static and declared at the class level.
3. The method, largestNumber(), takes three numbers as parameters and returns the largest of all the three numbers. Hence, the return type of the method is int and declared as static.
public static int largestNumber(int num1, int num2, int num3)
4. The largest number is found using multiple if-else statements.
5. The method, smallestNumber(), takes three numbers as parameters and returns the smallest of all the three numbers. Hence, the return type of the method is int and declared as static.
public static int smallestNumber(int num1, int num2, int num3)
6. The smallest number is found using multiple if-else statements.
7. Inside main(), an object of Scanner class is created to enable user input.
Scanner sc = new Scanner(System.in);
8. The user is prompted to enter three numbers.
9. The three numbers are passed as parameters to both the methods, largestNumber() and smallestNumber().
10. Two integer variables, largest and smallest are declared. These variables hold the values returned from largestNumber() and smallestNumber() methods, respectively and displayed to the user.