Startertutorials Blog
Tutorials and articles related to programming, computer science, technology and others.
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.
Home » Programming » Java » Programs » Basic » Java program to find given number is palindrome or not
Suryateja Pericherla Categories: Basic. No Comments on Java program to find given number is palindrome or not
0
(0)

In this article we will learn to implement a Java program to find given number is palindrome or not. A Java program is provided below which reads a number and prints whether it is a palindrome or not.

 

A palindrome is one which is same after reversing it. For example, if you take a number like 1221, after reversing it, it is again 1221.

 

Following program reads a number from the user and finds out whether it is a palindrome or not:

import java.util.Scanner;

public class Driver
{	
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		System.out.println("Enter a number: ");
		int n = input.nextInt();
		int dup = n;
		int rev = 0;
		while(n != 0)
		{
			rev =  rev * 10 + (n % 10);
			n = n / 10;
		}
		if(rev == dup)
			System.out.println("Given number is a palindrome");
		else
			System.out.println("Given number is not a palindrome");
		input.close();
	}
}

 

Input and output for the above program are as follows:

Enter a number: 
1225221
Given number is a palindrome

 


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Reply

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

Facebook
Twitter
Pinterest
Youtube
Instagram
Blogarama - Blog Directory