Share on Google+Share on Google+

Determining the largest number

This example of Java programming will teach you the coding for determining the largest number amongst three.

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");
  System.out.println("z is greater")
  System.out.println("y is greater");
  if (y>z){
  System.out.println("y is greater");

Download this example.


Posted on: June 1, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Determining the largest number   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
February 15, 2012

i wana help from u to learn java
ajit kumar karan
February 27, 2012

this side is very helpfull for me and it is the best side for question answer
April 3, 2012
Logic change

int x=50, y=70, z=90; The program is not working. We can use this logic if(x > y){ if(x > z){ System.out.println("X is highest"); } else { System.out.println("Z is highest"); } } else{ if(y > c){ System.out.println("Y is highest"); } else { System.out.println("C is highest"); } }
Santosh Shetty
May 14, 2012
coding confusion

int x=500, y=3000, z=4000; i tried this value and did not get the answer. i think the coding needs a change. ******************** class largernumber{ public static void main(String[] args) { int x=1000, y=200, 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{ if (y>z){ System.out.println("y is greater"); } else{ System.out.println("Z is greater"); } } } } *********** I m new to java programming and please bear with me and assist me incase i m wrong.
October 1, 2012

Insert X is lowest value, then Y is greater than X and Z is Greater Than Y.. Like x=100,y=500,c=3000 no output Programm is wrong :o
October 1, 2012

// Simple Logic public class Exper { public static void main(String args[]) { int a = 10; int b = 3; int c = 7; if((a>b) && (a>c)) { System.out.println("The Greatest Number Is A = " +a); } else if((b>a) && (b>c)) { System.out.println("The Greatest Number Is B = " +b); } else if((c>a) && (c>b)) { System.out.println("The Greatest Number Is B = " +c); } } }
November 1, 2012

I need to enter 6 grades and print the largest of the 6. I have the 6 grades entered and stored in variables but cannot find a way to print the highest and smallest. Also I am not allowed used methods. Any help would be greatfully appreciataed. Thank you.
July 1, 2013
about pgm

thnx for this progm
September 3, 2013

we are trying this program but its showing error in this pls clear my dioubt.