Tutorialspoint
Problem
Solution
Submissions

Check if a Number is a Palindrome

Certification: Basic Level Accuracy: 66.67% Submissions: 3 Points: 5

Write a C# program to check if a given integer is a palindrome. A palindrome number reads the same backward as forward.

Example 1
  • Input: x = 121
  • Output: true
  • Explanation:
    • Step 1: Check if the number is negative. It's not, so continue.
    • Step 2: Convert the integer to a string "121".
    • Step 3: Compare characters from both ends of the string, moving inward.
    • Step 4: All characters match, so the number is a palindrome.
Example 2
  • Input: x = -121
  • Output: false
  • Explanation:
    • Step 1: Check if the number is negative. It is, so it cannot be a palindrome due to the negative sign.
    • Step 2: Return false immediately as negative numbers can't be palindromes.
Constraints
  • The input is a 32-bit signed integer
  • Time Complexity: O(log n) where n is the input number
  • Space Complexity: O(1)
NumberControl StructuresIBMZomato
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Consider edge cases like negative numbers and zero.
  • You can convert the number to a string for easier comparison.
  • Alternatively, you can reverse the number by constructing a new integer.
  • Be careful about integer overflow when reversing.
  • Compare the reversed number with the original.

Steps to solve by this approach:

 Step 1: Check if the number is negative. If yes, return false as it cannot be a  palindrome.
 Step 2: Convert the integer to a string for easier comparison.
 Step 3: Initialize two pointers, one at the beginning and one at the end of the string.
 Step 4: Compare characters from both ends while moving the pointers inward.
 Step 5: If any pair of characters doesn't match, return false. Otherwise, return true.

Submitted Code :