Coding For Determining The Largest Number

Java Exercise : How to determinie the largest number among three. 

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");
      }
    }
  }

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.