JAVA PROGRAM TO CHECK PALINDROME OF A GIVEN NUMBER

PALINDROME NUMBER : A number that is same as the reverse of it is know as Palindrome Number. For Example 949, 878, 343, 545 etc

PALINDROME NUMBER ALGORTHM :

Algorithm

  • Take input from the user and store it in variable name num.

  • Pass this number to a method name findPalindrome() which we can can access directly
    by the className since it is a static method.

  • A loop is being used to iterate the text expression until n!=0

  • Reverse the Number and return the Number

  • Compare the returned number with the number that user has given, if both are same then
    its a palindrome else not.

Table of Contents

[hide]
  • Java Program to Check Palindrom of a Number using while loop
  • Java Program to Check Palindrom of a Number using for loop
  • Java Program to Check Palindrom of a Number using recursion

Example 1 : Java Program to Check Palindrom of a Number using while loop

copyimage
// Java Program to Check Palindrom of a Number using while loop
import java.util.Scanner;
class PalindromeDemo {
    public static int findPalindrome(int n) {
       int sum = 0, num = 0;
       while(n!=0) {
          num = n % 10;
          sum = (sum * 10) + num;
          n=n/10;
       }
       return sum;
    }
}

public class Codebator {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter any Number");
    int num = sc.nextInt();
    int result = PalindromeDemo.findPalindrome(num);
       if (num == result) {
       System.out.println("Palindrome");
       } else {
       System.out.println("Not a Palindrome");
       }
    }
}

OUTPUT :

Enter any Number868 Palindrome 

Example 2 : Java Program to Check Palindrom of a Number using for loop

copyimage
// Java Program to Check Palindrom of a Number using for loop
import java.util.Scanner;
class PalindromeDemo {
    public static int findPalindrome(int n) {
       int sum = 0, num = 0;
       for (; n != 0; n = n / 10) {
          num = n % 10;
          sum = (sum * 10) + num;
       }
       return sum;
    }
}

public class Codebator {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter any Number");
    int num = sc.nextInt();
    int result = PalindromeDemo.findPalindrome(num);
       if (num == result) {
       System.out.println("Palindrome");
       } else {
       System.out.println("Not a Palindrome");
       }
    }
}

OUTPUT :

Enter any Number756 Not a Palindrome 

Example 2 : Java Program to Check Palindrom of a Number using recursion

copyimage
// Java Program to Check Palindrom of a Number using recursion
import java.util.Scanner;
class PalindromeDemo {
    public static int findPalindrome(int n, int rev) {
        if (n == 0) 
        return rev; 
  
        rev = (rev * 10) + (n % 10); 
  
        return findPalindrome(n / 10, rev); 
    }
}

public class Codebator {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter any Number");
    int num = sc.nextInt();
    int result = PalindromeDemo.findPalindrome(num, 0);
       if (num == result) {
       System.out.println("Palindrome");
       } else {
       System.out.println("Not a Palindrome");
       }
    }
}

OUTPUT :

Enter any Number989 Palindrome 

Some useful links :