Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Determining the largest number
This example of Java programming will teach you the coding for determining the largest number amongst three.
 
Search Tutorials:
 
Software Solutions and Services
 

 
Google Custom Search:
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation

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.

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

12 comments so far (
post your own) View All Comments Latest 10 Comments:

there should be an else statment in the main else..

public static void main(String[] args)
{
int x=5,y=0,z=3;
if(x>y)
{
if(x>z)
System.out.println(x);
else
{
if(z>y)
System.out.println(z);
else
System.out.println(y);
}
}
else
{
if(y>z)
System.out.println(y);
else
System.out.println(z);
}
}

Posted by Champ on Sunday, 04.5.09 @ 01:03am | #86512

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

Training Courses
Tell A Friend
Your Friend Name
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.