-->

9/19/2019

Write a Java Program to find whether a string or number is palindrome or not.

1import java.util.Scanner;
2 
3public class Palindrome {
4    public static void main (String[] args) {
5        String original, reverse = "";
6        Scanner in = new Scanner(System.in);
7        int length;
8        System.out.println("Enter the number or String");
9        original = in.nextLine();
10        length = original.length();
11        for (int i =length -1; i>=0; i--) {
12            reverse = reverse + original.charAt(i);
13        }
14        System.out.println("reverse is:" +reverse);
15         
16        if(original.equals(reverse))
17            System.out.println("The number is palindrome");
18        else
19            System.out.println("The number is not a palindrome");
20         
21    }
22}

Output:
For String-
Enter the number or String
vijay
reverse is:yajiv
The number is not a palindrome
For Number-
Enter the number or String
99
reverse is:99
The number is palindrome
NEXT ARTICLE Next Post
PREVIOUS ARTICLE Previous Post
NEXT ARTICLE Next Post
PREVIOUS ARTICLE Previous Post