It has been a few weeks for me in Java. Yesterday I was trying to write a program to check if a given number is palindrome or not. I was able to write a program in which it asks the user to input the number of digits. It ran okay without any errors.
My question is, Is there any other way to do it without asking the user to input the number of digits? Kindly someone point out the logic and I will try out to write the program by myself.public class PalindromeNumberCheck { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("Enter the number to be checked : "); Scanner input = new Scanner(System.in); int num = input.nextInt(); int numOriginal = num; System.out.println("Enter the number of digits : "); int n = input.nextInt(); int reverse = 0; while(n != 0) { reverse = reverse + (num%10) * (int)Math.pow(10,(n-1)); num = num / 10 ; n--; } System.out.println("reverse = "+reverse); if(reverse == numOriginal) System.out.println("The number is a palindrome !"); else System.out.println("The number is not a palindrome !"); } }