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.

Examples:
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--) {
            revStr.append(str.charAt(i));
        }
        return revStr.toString();
    }
}

Program Output 1:

Please enter a number:
1234
Please enter a string:
radar

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

Program Output 2:

Please enter a number:
2332
Please enter a string:
java

 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


8 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.

Leave a Reply

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