# Comparing Two Numbers

This is a very simple example of Java that teaches you the method of comparing two numbers and finding out the greater one.

Tutorials

# Comparing Two Numbers

This is a very simple example of Java that teaches you the method of comparing two numbers and finding out the greater one. First of all, name a class "Comparing" and take two numbers in this class. Here we have taken a=24 and b=25, now we have to find out whether a=b, a>b or b>a. To find out this apply if and else condition one by one. Now apply the condition "if (a=b)", if this satisfies then type that both are equal in the system class. If this doesn't satisfy, then check whether a>b by applying the "else if" condition and type the message "a is greater than b" in the system class. Again this doesn't satisfy then 'else' condition as shown in the example will show that b is greater than a.

Now compile and run the program and you will find the desired output. If you are getting any error then check the whole program thoroughly and surely you will get correct result. By compiling and running this exact program, you will find that b is greater than a.

 ``` class  Comparing{   public static void main(String[] args) {   int a=24, b=25;   if (a == b){   System.out.println("Both are equal");   }   else if(a>b){   System.out.println("a is greater than b");   }   else{   System.out.println("b is greater than a");   }   } }```

`Download this example:`

# Comparing Two Numbers

Related Tutorials

Discuss: Comparing Two Numbers   View All Comments

Subject (*):

Wycliffe
June 13, 2011
COMMENT

your program is quite satisfying but would be better if a constructor was used
Chamomile
July 3, 2011
CaZoItrkGFsmjLNF

Hey, youÂ?re the goto expert. Thanks for hanigng out here.
ASISH SAMUEL ABRAHAM
October 11, 2011
JAVA LEARNING

WANT TO LEARN JAVA
targus
February 24, 2012
java comparing using if

I've been trying the above example and i've been trying to make it work but why there's an error with the If statement?
Gamer
March 16, 2012
java

i m loving it.
MD AKTARUZZAMAN
June 28, 2012
Even and Odd Program

import java.io.*; public class InpupOutput { /** * @param args */ public static void main(String[] args) { try{ System.out.println("Press Any Number for Output Even and even"); BufferedReader a =new BufferedReader(new InputStreamReader(System.in)); int nub=Integer.parseInt(a.readLine()); System.out.print("Even="); for(int i=1;i<=nub;i++) { if(i%2==0) { System.out.print(i+","); } } System.out.println(); System.out.print("Odd="); for(int k=1;k<=nub;k++) { if(k%2!=0) { System.out.print(k+","); } } } catch (Exception e) { } } }
Amit Kumar
July 18, 2012
COmputers

I M a Teacher at KV.
ragupathy
August 8, 2012
java

program as run in page get the answer
solomon
November 3, 2012