Palindrome Number

By   Tewodros   Date Posted: Mar. 16, 2024  Hits: 521   Category:  Algorithm   Total Comment: 0             A+ A-


side

Check if a given number is Palindrome. That is the number is the same when it reads forward and backward. 

Example

5665

Solution

If we know how to reverse a number then it is easy to check it.

public class Solution {

    public bool IsPalindrome(int x) {

         if(x<0) return false;

        if(x == reverse(x)) return true;

       

        return false;

    }

   

    int reverse(int n)

    {

        int res = 0;

        while(n != 0)

        {

            res = res * 10 + n%10;

            n=n/10;

        }

        return res;

    }

}


Tags



Back to Top



Related Blogs






Please fill all fields that are required and click Add Comment button.

Name:*
Email:*
Comment:*
(Only 2000 char allowed)


Security Code:* gznrxz

Back to Top