Greatest4.java
Aim:
To find Greatest among 4 numbers using nested-if
Source code:
import java.util.*;
import java.lang.*;
class Greatest4
{
public static void main(String args[])
{
int a,b,c,d;
Scanner input=new Scanner(System.in);
System.out.println("Enter values for a,b,c,d");
a=input.nextInt();
b=input.nextInt();
c=input.nextInt();
d=input.nextInt();
if(a>b)
{
if(a>c)
{
if(a>d)
System.out.println("a is greater");
else
System.out.println("d is greater");
}
else
{
if(c>d)
System.out.println("c is greater");
else
System.out.println("d is greater");
}
}
else
{
if(b>c)
{
if(b>d)
System.out.println("b is greater");
else
System.out.println("d is greater");
}
else
{
if(c>d)
System.out.println("c is greater");
else
System.out.println("d is greater");
}
}
}
}
Output-1:
Enter values for a,b,c,d
2
4
5
9
d is greater
Output-2:
Enter values for a,b,c,d
12
35
47
11
c is greater
Output-3:
Enter values for a,b,c,d
5
12
6
8
b is greater
Output-4:
Enter values for a,b,c,d
455
-7
12
341
a is greater
No comments:
Post a Comment