Home Java Beginners Determining the largest number
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Determining the largest number

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

Advertisement

Liked it!  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.

Posted on: June 1, 2007

Ask Questions?    Discuss: Determining the largest number   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
shruti
June 26, 2011
same question

this is a long method how can we apply the tertiary(?:) operator on this ........
Rishi
November 17, 2011
missing code

hi, while learning from your site i have found that you didn't mention what will happen if i changed the value of variable y = 700 please update your program Regards Rishi
balaji
December 26, 2011
computer science

send me java porgramming software free dowloand in my emailid pls sendme
sivaprasad
January 16, 2012
java

awesome
shiva
February 15, 2012
java

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

this side is very helpfull for me and it is the best side for question answer
imperfect
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.
Omi
October 1, 2012
Java

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
Omi
October 1, 2012
java

// 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); } } }
smartie92
November 1, 2012
assignment

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.
gopal
July 1, 2013
about pgm

thnx for this progm
ajuram
September 3, 2013
doubt

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