Java program to check if a number or word is palindrome

package com.physicsinfo;

import java.util.Scanner;

public class Main{
    public static void main(String args[]){
        int flag=0;
        System.out.print("Enter a number or word and I'll tell you if it's palindrome: ");
        Scanner sc = new Scanner(System.in);
        String palin = sc.next();
        int n = palin.length();
        char[] character = new char[n];
        for (int i=0;i<n;i++){
            character[i] = palin.charAt(i);
        }
        for (int j=0;j<=n/2;j++){
         if(character[j]!=character[n-j-1]){flag=1;break;}
        }
        if(flag==0){
            System.out.println("It's Palindrome.");
        }
        else {
            System.out.println("It's not Palindrome.");
        }
    }
}

Leave a Comment

Your email address will not be published.