Java Program to check Number is Palindrome

Ā 

Write a Java Program to check whether the Number is Palindrome or Not – Java Tutorial

Problem Statement

 Write a Java Program to check whether the Number is Palindrome or Not.

Solution:

A number is said to be palindrome if its reverse is equal to the original number.

For example, 123 is not a palindrome, as it’s reverse 321 is not equal to 123. The number 121 is a palindrome as its reverse 121 is equal to 121.

Video Tutorial:

Step to write the palindrome program in Java.

First, create a class named Palindrome and add the main method to it.

Next, set the value of rev to 0.

Read the number to be reversed from the user using an object of scanner class object.

Repeat the following three steps repeatedly until the value of num is greater than 0.

1. rem = num % 10

2. rev = rev*10 + rem

3. num = num / 10

Finally, check whether the reverse of the number is equal to its original.

If the value of reverse is equal to its original then print, the number is palindrome or print the number is not a palindrome using the statement System.out.println

import java.util.Scanner;

/*
 * Write a Java Program to check whether a given number in palindrome or not
 *  
 * Subscribe to Mahesh Huddar YouTube Channel for more Videos
 * 
 * rev = 0
 * num = 123
 * 
 * 1. rem = num % 10          3       2       1
 * 2. rev = rev*10 + rem      3       32      321
 * 3. num = num / 10          12      1       0
 * 
 * if rev is same as num then number is palindrome
 */
public class Palindrome {

	public static void main(String[] args) 
	{
		int rev = 0, num, org, rem;
		Scanner in = new Scanner(System.in);
		System.out.println("Enter a number:");
		num = in.nextInt();
		org = num;
		while(num>0)
		{
			rem = num % 10;
			rev = rev*10 + rem;
			num = num / 10;
		}
		if (org == rev)
		{
			System.out.println("The " + org + " is a palindrom");
		}
		else
		{
			System.out.println("The " + org + " is not a palindrom");
		}
	}
}
 

The output of the Factorial Program in Java

Case 1:

Enter a number: 123

The 123 is not a palindrome

Case 2:

Enter a number: 121

The 121 is a palindrome

Summary:

In this article, we understood How to write a Java Program to get the Reverse of a Number – Java Tutorial. If you like the tutorial share it with your friends. Like the Facebook page for regular updates and the YouTube channel for video tutorials.

Leave a Comment

Your email address will not be published. Required fields are marked *

Welcome to VTUPulse.com


Computer Graphics and Image Processing Mini Projects -> Click Here

Download Final Year Project -> Click Here

This will close in 12 seconds