Java Program to Check Original equal to its Reverse or Not

This article is created to cover a program in Java that checks whether the reverse of a number is equal to its original or not.

Other recommended programs, based on this, are:

The question is, write a Java program to check whether a number is equal to its reverse or not. Here is its answer:

import java.util.Scanner;

public class CodesCracker
{
   public static void main(String[] args)
   {
      int num, orig, rem, rev=0;
      Scanner scan = new Scanner(System.in);
      
      System.out.print("Enter the Number: ");
      num = scan.nextInt();
      
      orig = num;
      while(num>0)
      {
         rem = num%10;
         rev = (rev*10) + rem;
         num = num/10;
      }
      
      if(orig==rev)
         System.out.println("\nYes, the number is equal to its reverse.");
      else
         System.out.println("\nNo, the number is not equal to its reverse.");
   }
}

The snapshot given below shows the sample run of above program, with user input 12321:

java program check reverse equal original

Here is another sample run with user input 12345:

java find reverse equal original

Java Online Test


« Previous Program Next Program »