Greatest of any three numbers using nested if:
User enter any three number and press enter button to show the greater number among the three numberimport java.util.*;
class Greatest
{
public static void main(String args[])
{
int a,b,c;
Scanner rd = new Scanner(System.in);
System.out.println("Enter three numbers : ");
a = rd.nextInt();
b = rd.nextInt();
c = rd.nextInt();
if(a>b)
{
if(a>c)
{
System.out.println( a+" is the greatest no.");
}
else
{
System.out.println(c+" is the greatest no.");
}
}
else
{
if(b>c)
{
System.out.println(b+" is the greatest no.");
}
else
{
System.out.println(c+" is the greatest no.");
}
}
}
}
Output:
Enter three numbers :25 48 90
90 is the greatest no.
87 is the greatest no.
90 is the greatest no.
Greatest of any three numbers using else if ladder:
User enter any three number and press enter button to show the greater number among the three number.import java.util.*;
class Greatest
{
public static void main(String args[])
{
int a,b,c;
Scanner rd = new Scanner(System.in);
System.out.println("Enter three numbers : ");
a = rd.nextInt();
b = rd.nextInt();
c = rd.nextInt();
if(a>b && a>c)
{
System.out.println( a+" is the greatest no.");
}
else if(b>a && b>c)
{
System.out.println(b+" is the greatest no.");
}
else
{
System.out.println(c+" is the greatest no.");
}
}
}
Output:
Enter three numbers : 55 87 1287 is the greatest no.
Greatest of any three numbers using ternary operator:
User enter any three number and press enter button to show the greater number among the three number.import java.util.*;
class Greatest
{
public static void main(String args[])
{
int a,b,c;
Scanner rd = new Scanner(System.in);
System.out.println("Enter three numbers : ");
a = rd.nextInt();
b = rd.nextInt();
c = rd.nextInt();
int max=(a>b)?(a>c)?a:c:(b>c)?b:c;
System.out.println( max+" is the greatest no.");
}
}
Output:
Enter three numbers :60 45 33
60 is the greatest no.
60 is the greatest no.
0 Comments