Determining the largest number

This example of Java programming will teach you the
coding for determining the largest number amongst three. Here we have taken
three integers as x = 500, y = 70 and z = 3000. After defining these three
integers under the class "largernumber" apply "if" and
"else" conditions that can help you in finding the largest value one
by one.
First check if "x>y". If this
satisfies then check whether x>z or not. Again if this satisfies then write
in the system class that "x is greater". Again the term
"else" comes when "x" is not greater than "z". So
check again, if "z" is greater than "y" or not. If this
satisfies then type in the system class as "z is greater" otherwise
(in the else condition) "y" is greater. Now check whether
"y" is greater than "z" or not.
If "x" is not greater than
"y" as per the first condition, then the condition "else"
comes and now you have to check if "y>z" or not. If this satisfies
then the output comes as "y is greater".
Don't get confuse and analyze every condition
one by one and follow this example.
Here is the code of program:
class largernumber{
public static void main(String[] args) {
int x=500, y=70, z=3000;
if (x>y){
if (x>z){
System.out.println("x is greater");
}
else{
if(z>y){
System.out.println("z is greater");
}
else{
System.out.println("y is greater");
}
}
}
else{
if (y>z){
System.out.println("y is greater");
}
}
}
}
|
Download this example.

|
Current Comments
11 comments so far (post your own) View All Comments Latest 10 Comments:This is another type of example for finding the largest number.
class velu1
{
public static void main(String args[])
{
int a=3,b=1,c=4;
if (a>b && a>c)
{
System.out.println("a is greatest");
}
else
if (b>c && b>a)
{
System.out.println("b is greatest");
}
else
if (c>a && c>b)
{
System.out.println("c is greatest");
}
}
}
Posted by veluswamy on Saturday, 08.23.08 @ 22:41pm | #74656
For example,
If x = 2, y = 200 and z = 400
this program does not produce the output as it never compares y and z separately.
I think another line of code should be added as in:
class LargerNumber{
public static void main(String[] args) {
int x=2, y=70, z=3000;
if (x>y){
if (x>z){
System.out.println("x is greater");
}
else{
if(z>y){
System.out.println("z is greater");
}
else{
System.out.println("y is greater");
}
}
}
else if (y>z){
System.out.println("y is greater");
}
else
System.out.println("z is greater");
}
}
Posted by Anuradha Mundinamani on Saturday, 08.2.08 @ 16:46pm | #70536
in above this program we are initiating no to variables other than this declare only variables and compare and they should read the values at compilation time ao simply what is the syntax for input of charater or nos.
Posted by lavanya on Monday, 04.28.08 @ 22:27pm | #58083
thanks a lot
Posted by pankaj jha on Thursday, 02.7.08 @ 14:40pm | #47504
class greaternumber
{
public static void main(String[] args)
{
int x=100,y=200,z=12;
if (x>y && y>z)
{
System.out.println("x is the Greater number");
}
else if(z>x && z>y)
{
System.out.println ("Z is the larget number");
}
else
{
System.out.println("Y is the larger number");
}
}
}
Posted by mangalamalatkar on Wednesday, 04.25.07 @ 16:40pm | #14917
BEING FRIENDLY AM HAPPY THAT NOW I CAN DO JUST A THING WITH JAVA.IT TAKES ME MORE THAN 3 WEEKS NOW TRYING TO UNDERSTAND WHAT WAS BEING TOUGHT BUT TODAY &I MEAN THIS DAY AM CAPABLE....KEEP ON HELPING US..THANX A LOT
Posted by l'ness on Thursday, 04.12.07 @ 00:05am | #13911
If the task is display the largest number among 100 integers, then what would be the solution, should we folow IF condition or Loop or something else please send the coding or the logic if anyone could solve this?
Posted by Shajib on Thursday, 03.15.07 @ 23:02pm | #11839
please explain me about (string[] args) in the main method
Posted by pranitha on Tuesday, 02.20.07 @ 04:52am | #8421
Thank you very much for setting up the example and sharing the code. I feel there may be some redundancy after the first else statement.
Best Regards,
Kim
Posted by Kim Tran on Thursday, 02.1.07 @ 16:04pm | #5204
ya this example is too good , but if it is some what diffecult that will be better ok eny way thanks for this examples
Posted by sharath pawar on Tuesday, 01.23.07 @ 16:46pm | #4143