Determining the largest number

Ads
 

Determining the largest number

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

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.

Ads

Ads