In this tutorial, we will compare two integers with each other and find which one is bigger, smaller or they are equal
Sorting and Comparing Two Numbers in Java
there are 3 scenarios in comparing two numbers with each other
1. a>b
2. a<b
3. a=b
Let's write a program that takes two numbers from user and compares these numbers, taking into account the three scenarios above
Scanner input = new Scanner(System.in); System.out.println("What is the first number?"); int a = input.nextInt(); System.out.println("What is the second number?"); int b = input.nextInt(); if (a > b) System.out.println(a+">"+b); else if (a < b) System.out.println(a+"<"+b); else System.out.println(a+"="+b);
Output Scenario 1:
What is the first number? 7 What is the second number? 6 7>6
Output Scenario 2:
What is the first number? 2 What is the second number? 5 2<5
Output Scenario 3:
What is the first number? 3 What is the second number? 3 3=3