write a program to find the greatest and smallest of 3 numbers using nested if in java
Answers
The given problem is solved using Java.
import java.util.Scanner;
public class MaxMin{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int a,b,c,max,min;
System.out.print("Enter first number: ");
a=sc.nextInt();
System.out.print("Enter second number: ");
b=sc.nextInt();
System.out.print("Enter third number: ");
c=sc.nextInt();
sc.close();
if(a==b && b==c)
System.out.println("All are equal.");
else{
if(a>b){
if(c>b)
min=b;
else
min=c;
if(a>c)
max=a;
else
max=c;
}
else{
if(a<c)
min=a;
else
min=c;
if(b>c)
max=b;
else
max=c;
}
System.out.println("Largest Number: "+max);
System.out.println("Smallest Number: "+min);
}
}
}
See the attachment for output.