In this tutorial you will learn about the Java Program to check Equal Number in Java and its application with practical example.
In this tutorial, we will learn to create a Java Program to check Equal Numbers in Java using java programming.
Prerequisites
Before starting with this tutorial, we assume that you are best aware of the following Java programming concepts:
- Java Operators.
- Basic Input and Output function in Java.
- Class and Object in Java.
- Basic Java programming.
- If-else statements in Java.
How do you equal a number in Java?
How to compare integer values are equal or not in Java, we can use either == (operator) or we can simply subtract these two values and values becomes zero means given number are equal. Both are used to compare two values. We can also use the equal() method to find numbers are equal or not.
Example How to check whether they are equal or not, use equal to (== )operator.
a == b
Example2 How to check whether they are equal or not, use the equal to ( – )operator.
if(a -b==0)they are equal other wise they are not equal..
Java Program to check Equal Number in Java
In this program, we will take two values from the user and print numbers are an equal or not the smallest element in an array using a nested for loop. We would first declare and initialized the required variables. Next, we will find the smallest number in an array. Let’s have a look at the code.
Using relational operator equal to (==) in java program..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
// Check Two Integers are Equal or Not in Java // using relational operator equal to(==) in java program.. import java.util.Scanner; public class Equal_Int { public static void main(String[] args) { int a, b; Scanner s = new Scanner(System.in); System.out.print("Enter first number:"); a = s.nextInt(); System.out.print("Enter second number:"); b = s.nextInt(); if(a == b) { System.out.println(a+" and "+b+" are equal "); } else { System.out.println(a+" and "+b+" are not equal "); } } } |
Output
Equal
Not Equal
using arithmetic operator equal to(-) in java program..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
// Check Two Integers are Equal or Not in Java // using arithmetic operator in java program.. import java.util.Scanner; import java.io.*; class Equal_to_int { public static void main(String[] args) { //decalring variables andtaking values form user.. Scanner sc= new Scanner(System.in); //System.in is a tandard input stream. System.out.print("Enter two numbers- "); int a= sc.nextInt(); int b= sc.nextInt(); // checking conditon for given integer are equal or not... if ((a - b) == 0) //if ture.... System.out.println("Numbers are equal"); else // if flase... System.out.println("Numbers are not equal"); } } |
Output
Equal
Not Equal
In the above program, we have first declared and initialized a set variables required in the program.
- a= it will hold value of first variable.
- b= it will hold value of second variable.
- i and j for iteration
- temp = it will hold temporary value of an array.
After declaring variables we initiate values and take value from user at run time.
then with the help of relational and arithmetic operator we will find given integer are equal or not.
using arithmetic operator equal to(-) in java program..
Using relational operator equal to (==) in java program..
and finally we will print the results…