Palindrome Numbers - Check if number is Palindrome

PHOTO EMBED

Sun Feb 06 2022 02:42:58 GMT+0000 (UTC)

Saved by @Uttam #java #mathematics #lecture #gfg #geeksforgeeks #checkpalindrome

// Time Complexity: O(logN), Auxiliary Space: O(1)
import java.io.*;
import java.util.*;

public class CheckPalindrome {

	static boolean isPal(int n)
	{
		int rev = 0;

		int temp = n;
		// reversed integer is stored in reversed variable
		while(temp != 0)
		{
			int ld = temp % 10;

			rev = rev * 10 + ld;

			temp = temp / 10;
		}	
		// palindrome if orignal and reversed are equal
		return rev==n;
	}

	public static void main (String[] args) {
		
		int number = 4553;

		System.out.println(isPal(number));

	}
}
content_copyCOPY

Let the given number be num. A simple method for this problem is to first reverse digits of num, then compare the reverse of num with num. If both are same, then return true, else false. Palindrome number algorithm - Get the number to check for palindrome - Hold the number in temporary variable - Reverse the number - Compare the temporary number with reversed number - If both numbers are same, print "palindrome number" - Else print "not palindrome number"