Tutorialspoint
Problem
Solution
Submissions

Check if a Number is a Palindrome

Certification: Basic Level Accuracy: 69.23% Submissions: 13 Points: 10

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

Example 1
  • Input: number = 121
  • Output: true
  • Explanation:
    • Step 1: Read the number from right to left.
    • Step 2: Compare with the original number.
    • Step 3: Since both are the same, return true.
Example 2
  • Input: number = 123
  • Output: false
  • Explanation:
    • Step 1: Read the number from right to left, which gives 321.
    • Step 2: Compare with the original number 123.
    • Step 3: Since they are different, return false.
Constraints
  • -2^31 ≤ number ≤ 2^31 - 1
  • Time Complexity: O(n), where n is the number of digits in the number
  • Space Complexity: O(1)
NumberControl StructuresTech MahindraeBay
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

  • Convert the number to a string and compare it with its reverse.
  • Use arithmetic operations to reverse the number and compare it with the original number.
  • Handle negative numbers separately since they cannot be palindromes.

Steps to solve by this approach:

 Step 1: Define a function is_palindrome that takes an integer number.
 Step 2: Handle edge case: negative numbers cannot be palindromes.
 Step 3: Initialize variables for the reversed number and the original number.
 Step 4: Reverse the number by extracting digits from right to left.
 Step 5: Compare the reversed number with the original number.
 Step 6: Return true if they match, false otherwise.
 Step 7: In main, call is_palindrome with a number and print the result.

Submitted Code :