Java program to find Largest of Three Numbers!

Java program to find largest numbers !


In this program, you'll learn to find the largest among three numbers using if else and nested if..else statement in Java.



Example 1: Find largest of Three number !

Code:


public class Three_Number {

public static void main(String[] args) {
// TODO Auto-generated method stub
int a=30; 
int b=40;
int c=50;
if(a>b && a>c)
{
System.out.println("a is largest: "+a);
}
else if(b>a && b>c)
{
System.out.println("b is largest: "+b);
}
else
System.out.println("c is largest: "+c);

}
}

When you run the program, the output will be:

c is largest: 50

        *-----------------------------------------------------------------*
Example 2: Find the largest  of Five Number:
Code:
public class Five_number {

public static void main(String[] args) {
// TODO Auto-generated method stub
int a=3077;
int b=309;
int c=306;
int d=305;
int e=340;

if(a>b && a>c && a>d && a>e)
{
System.out.println("a is largest: "+a);
}

else if(b>a && b>c && b>d && b>e)
{
System.out.println("b is largest: "+b);
}

else if(c>a && c>b && c>d && c>e)
{
System.out.println("c is largest: "+c);
}
else if(d>a && d>b && d>c && d>e)
{
System.out.println("d is largest: "+d);
}
else 
System.out.println("e is largest: "+e);

}
}

When you run the program, the output will be:

a is largest: 3077


        %--------------------------------------------------------------%

In the above program, instead of checking for two conditions in a single if statement, we use nested if to find the greatest.


learn-java-programming



*

إرسال تعليق (0)
أحدث أقدم