Jun 10

PalindromeIn this core java beginners tutorial, let us write a simple java program to check whether a given number or string is palindrome or not without using built-in reverse() method of StringBuilder or StringBuffer. Later we can execute the program and check the output for different numbers and strings.

What is a palindrome Number or String?

A Number or String whose meaning may be interpreted the same way in either forward or reverse direction is known as Palindrome Number or String.

Palindrome Numbers: 1221, 595, 82328.
Palindrome Strings: Liril, Radar, Level, Mom, Dad, Malayalam, Civic, Madam, Bob, Pop, Refer, Rotator, Stats.

Example Java Program to check whether a given number or string is palindrome or not

import java.util.*;

public class Palindrome 
    public static void main(String[] args) 
        Scanner scanner = new Scanner(System.in);

        System.out.println("Please enter a number: ");
        int num = scanner.nextInt();
        System.out.println("Please enter a string: ");
        String str = scanner.next();

        Palindrome palin = new Palindrome();
        int revNum = palin.reverse(num);
        String revStr = palin.reverse(str);

        if (num == revNum) {
            System.out.printf("\n The number %d is a Palindrome ", num);
        } else {
            System.out.printf("\n The number %d is not a Palindrome ", num);
        if (str.equalsIgnoreCase(revStr)) {
            System.out.printf("\n The string '%s' is a Palindrome ", str);
        } else {
            System.out.printf("\n The string '%s' is not a Palindrome ", str);
    // Method to return the reverse of a number
    public int reverse(int num) {
        int revNum = 0;
        while (num > 0) {
            int rem = num % 10;
            revNum = (revNum * 10) + rem;
            num = num / 10;
        return revNum;
    // Method to return the reverse of a string
    public String reverse(String str) {
        StringBuilder revStr = new StringBuilder();
        for (int i = str.length()-1; i >= 0; i--) {
        return revStr.toString();

Program Output 1:

Please enter a number:
Please enter a string:

 The number 1234 is not a Palindrome
 The string 'radar' is a Palindrome

Program Output 2:

Please enter a number:
Please enter a string:

 The number 2332 is a Palindrome
 The string 'java' is not a Palindrome 

If you are not sure about Scanner class and reverse number or string algorithm read Java Program to Find Reverse of a Given Number and String

In this program, we prompt the user to enter a number & a string. By using pre-written reverse methods, we got reverse number and reverse string. Then we are simply comparing the original number & string with their counterparts. If they are same, they are called Palindrome.

For more java programs, visit Sample Programs for Beginners to Learn Java Programming Quickly & Easily.

Related posts:

  1. Write a Java Program to Find Reverse of a Given Number and String with Output
  2. Write a Java Program to Check whether a Number is Prime or Not with Output
  3. Write a Java Program to Display all Prime Numbers upto Given Number using for loop
  4. Explain String vs StringBuffer vs StringBuilder with toString() Example
  5. Is String Mutable in Java? Why Wrapper Classes are Immutable and Final in Java?
  6. What is a Java Exception? Explain Exception Handling in Java with Examples Program
  7. How to fix “Exception in thread main java.lang” Common Exceptions? – Tutorial 1
  8. Java 5 (J2SE 5.0/JDK 1.5) New Features with Examples Tutorial 2
  9. Java Character Wrapper Class Tutorial: API Methods and Constructors
  10. Java Integer Wrapper Class Tutorial: API Methods and Constructors

written by Seetha Ram Janapala
Tags: core java for beginners, core java tutorial, j2se, j2se 5.0, java programming scanner, java strings, palindrome, palindrome algorithm, palindrome check with output, palindrome examples, palindrome java program, palindrome number, palindrome string, reverse of a number, reverse of a string, StringBuilder, stringbuilder examples

21 Responses to “Write a Java Program to Check a Number/String is Palindrome or Not with Output”

  1. 1. google adwords certification Says:

    Hi there, I enjoy reading all of your article. I like
    to write a little comment to support you.

  2. 2. provillus Says:

    I love your blog.. very nice colors & theme. Did you make this
    website yourself or did you hire someone to do it for you?
    Plz reply as I’m looking to create my own blog and
    would like to know where u got this from. thanks

  3. 3. Las Vegas Microwave Service Says:

    Wow, fantastic blog layout! How long have you been blogging for?

    you make blogging look easy. The overall look of your web site is magnificent, as well
    as the content!

  4. 4. hotel slippers Says:

    Greetings! Very helpful advice in this particular article!

    It is the little changes that will make the
    biggest changes. Thanks a lot for sharing!

  5. 5. spilleautomater freespins Says:

    Hi there colleagues, its enormous article on the topic of teachingand entirely defined, keep it up all the time.

  6. 6. localbizprofit scam Says:

    It’s an remarkable paragraph in favor of all the web users;
    they will get advantage from it I am sure.

  7. 7. philips philips songs Says:

    Do you mind if I quote a couple of your articles as long as I
    provide credit and sources back to your webpage?
    My blog site is in the very same niche as yours and my users would genuinely benefit from some of the information you present here.
    Please let me know if this okay with you. Thank you!

  8. 8. Forex trading Ideas Says:

    I’ve learn several just right stuff here. Certainly worth bookmarking for revisiting.
    I wonder how a lot attempt you set to make one of these fantastic informative site.

  9. 9. kolikkopelit netissa Says:

    Spot on with this write-up, I honestly think this website needs far more attention.
    I’ll probably be returning to see more, thanks for
    the info!

  10. 10. cash loan today Says:

    It’s remarkable in support of me to have a site,
    which is valuable for my knowledge. thanks admin

  11. 11. clash of clans hack free Says:

    Very nice article. I certainly love this site. Stick with it!

  12. 12. Thierry Lasry Lunettes Says:

    It’s very trouble-free to find out any matter on net as compared to
    textbooks, as I found this article at this web site.

  13. 13. fix-my-hard-drive.com Says:

    It’s appropriate time to make some plans for tthe future and it is time
    to be happy. I have read this post and if I could I want to suggest you few interesting things or suggestions.
    Maybe you can write next articles referring to this article.
    I desire to read more things about it!

  14. 14. www.uk-data-recovery.co.uk Says:

    It’s hard to find experienced people in this particular subject, however,
    you sound like you kbow what you’re talking about!


  15. 15. Maria Says:

    Hello there, just became aware of your blog through Google,
    and found that it’s truly informative. I’m going to watch out for brussels.

    I’ll appreciate if you continue this in future. Lots of people will be
    benefited froom your writing. Cheers!

  16. 16. Juli Says:

    Hello, I would like to subscribe for this web site to take latest updates, thus where can i do
    it please assist.

  17. 17. Muoi Says:

    Nice blog here! Also your website loads up fast! What host are you using?

    Can I get your affiliate link to your host?

    I wish my site loaded up as quickly as yours lol

  18. 18. jay Says:

    i suggest also showing unit test for the logic examples

  19. 19. home based business Says:

    Hey there! Would you mind if I share your blog with my zynga group?
    There’s a lot of people that I think would really enjoy your content.
    Please let me know. Thanks

  20. 20. ravindra Says:

    very good to me for knowledge updation

  21. 21. ravindra Says:

    like to us

Leave a Reply

Copyright © 2014 Explain-Java.com. All rights reserved.