Reverse Five Digits Number Check Equal or Not in Java


A five-digit number is entered through the keyboard. Write a program to obtain the reversed number and to determine whether the original and reversed numbers are equal or not


The program starts by creating a Scanner object to read input from the user. It then prompts the user to enter the digits and reads the integer input using the nextInt() method of the Scanner class.

Next, the program initializes variables to store the remainder of the input integer when divided by 10, the sum of the digits in the reversed integer, and a copy of the original input integer. It then uses a while loop to reverse the input integer by repeatedly extracting its last digit using the modulus operator %, adding it to the sum after shifting it one place to the left using multiplication, and dividing the input integer by 10 to remove its last digit.

After reversing the input integer, the program compares it with the original input integer using the == operator. If they are equal, it prints "Equal.." to the console. Otherwise, it prints "Not Equal..".

Note that the program assumes that the input integer is non-negative. If the user enters a negative integer, the program will still attempt to reverse it but may produce unexpected results.

Source Code

import java.util.Scanner;
class Reverse_EqualNot
{
	public static void main(String args[])
	{
		Scanner input = new Scanner(System.in);
		System.out.print("Enter the Digits :");
		int num = input.nextInt();
		int rem,sum = 0,a = num;
		while(num>0)
		{
			rem=num%10;
			sum=(sum*10)+rem;
			num=num/10;
		}
		if(a == sum)
			System.out.print("Eqaul..");
		else			
			System.out.print("Not Eqaul..");
	}
}

Output

Enter the Digits :548319
Not Eqaul..

Enter the Digits :1254521
Eqaul..

Example Programs