1. Write a program in Java to input three numbers and display the greatest and
the smallest of the two numbers.
Hint: Use Math.min() and Math.max()
Sample Input: 87, 65, 34
Sample Output: Greatest Number 87
Smallest number 34
Dit
Answers
Answer:
public class LargestAndSmallest {
2
3
public void fin(int a, int b, int c) {
4
int max = a;
5
if (b > max) {
6
max = b;
7
}
8
if (c > max) {
9
max = c;
10
}
11
int min = a;
12
if (b < min) {
13
min = b;
14
}
15
if (c < min) {
16
min = c;
17
}
18
System.out.println("Largest of the three numbers is " + max);
19
System.out.println("Smallest of the three numbers is " + min);
20
}
21
}
Explanation:
hope it helps
thanks for asking questions
Following are the program is given below
Output:
Please enter the first number:
87
Please enter the second number:
65
Please enter the third number:
34
Greatest Number:87
Smallest Number:34
Explanation:
import java.util.*; // import package
public class Main
{
public static void main(String args[]) // main class
{
Scanner sc3= new Scanner(System.in); // creating the object of scanner class
System.out.println("Please enter the first number:");
int a1= sc3.nextInt(); // taking input by user
System.out.println("Please enter the second number:");
int a2= sc3.nextInt(); // taking input by user
System.out.println("Please enter the third number:");
int a3= sc3.nextInt(); // taking input by user
int t1=Math.max(a1,a2); // find the maximum number between the two
System.out.println("Greatest Number:" +Math.max(t1, a3)); // print the //greatest number
int t2=Math.min(a1,a2); // find the mainimum number between the two
System.out.println("Smallest Number:" +Math.min(t2, a3)); // print the //smallest number
}
}
Following are the description of program
- Creating an object of Scanner class i.e "sc3 " for taking input from the user.
- Taking the int input a1,a2,a3 variable of int type with the help of scanner class object.
- Firstly finding the greatest between the two numbers "a1" and "a2 by using math.max() this function finds the greatest number between the 2 number" and store them into "t1" variable after that compare this value with the variable "a3" to finding the greatest number.
- After that calculating the smallest number by comparing the two numbers "a1" and "a2" by using math.min() function this function finds the smallest number between the 2 number and store them into "t2" variable after that compare this value with the variable "a3" to finding the smallest number.
Learn More:
- brainly.in/question/4959763