Palindrome Number

Abhishek KumarAbhishek Kumar
1 min read

Reads the same from forward and backward this is called Palindrome Number.

Given an integer x, return true if x is a palindrome, and false otherwise.

class Solution

{ public boolean isPalindrome

(int x) { if(x<0){ return false ; } int n = x; int revnum = 0; while(n>0){ int d = n%10; revnum = revnum*10 +d; n = n/10; } if (revnum == x){return true ; }else{ return false ; } } }

x = 121 n = 121 revnum = 0 d = 1 (010+1)revnum = 1 n= n/10(121/10) = 12 (12/%10) d=2 (110+2)revnum = 12 n= 1 d = 1 revnum = 121 n= 0 (0>0) output False <revnum == x) True output >>> True

A number is called a palindrome if it reads the same from forward and backward. Examples: Explanation: 12321 is a palindrome number because it reads same forward and backward. Explanation: 1234 is not a palindrome number because it does not read the same forward and backward.

0
Subscribe to my newsletter

Read articles from Abhishek Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Abhishek Kumar
Abhishek Kumar