1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
/* Compare Two Numbers Java Example This Compare Two Numbers Java Example shows how to compare two numbers using if else if statements. */ public class CompareTwoNumbers { public static void main(String[] args) { //declare two numbers to compare int num1 = 324; int num2 = 234; if(num1 > num2){ System.out.println(num1 + " is greater than " + num2); } else if(num1 < num2){ System.out.println(num1 + " is less than " + num2); } else{ System.out.println(num1 + " is equal to " + num2); } } } /* Output of Compare Two Numbers Java Example would be 324 is greater than 234 */ |
//Another method using ? Operator
public class CompareTwoNumbers
{
public static void main(String Args[])
{
int num1,num2,temp;
num1=10;
num2=20;
temp = (num1 > num2) ? num1 : (num1 == num2) ? 0 : num2; /*If (num1>num2) num1 will be printed. Else if (num1==num2) 0 will be printed. Else num2 is printed.*/
System.out.println(“Greater no. is “+temp);
}
}
import java.util.Scanner;
public class CompareTwoNumber {
public static void main(String[] args) {
System.out.println(“enter First number”);
Scanner input = new Scanner(System.in);
int mum1 = input.nextInt();
System.out.println(“enter the second number”);
int mum2 = input.nextInt();
if(mum1>mum2){
System.out.println(mum1+”is geater than”+mum2);
}
else if(mum2>mum1){
System.out.println(mum2+ “is greater than”+ mum1);
}
else
System.out.println(mum1 + “are equal” +mum2);
}
}