write a java program to input three numbers. Print the highest and lowest of them assuming different numbers are input . answer it fast
Answers
Question:-
write a java program to input three numbers. Print the highest and lowest of them.
_________________________
Code:-
import java.util.*;
class Highest
{
public static void main()
{
Scanner sc = new Scanner (System.in);
int a=sc.nextInt(),b=sc.nextInt();
int c=sc.nextInt();
int larg=Math.max(a,Math.max(b,c));
int sma=Math.min(a,Math.min(b,c));
System.out.println("Greatest:-"+larg);
System.out.println("Smallest:-"+sma);
}
}
________________________
Input:- 4,5,6
Output:-
Greatest:-6
Smallest:-4
- Write a java program to input 3 numbers and print the highest and lowest number.
- If we use the Math functions of java, then the code can be made much shorter. In this approach,we will use math functions only. Here is the source code.
import java.util.*;
class MaxMin
{
public static void main(int a, int b, int c)
{
int max=Math.max(a, Math.max(b, c));
int min=Math.min(a, Math. min(b, c));
System.out.println("Largest Number: "+max);
System.out.println("Smallest Number: "+min);
} // end of main() method.
} // end of class.