Palindrome number in C

Sandeep kamila
Last Updated: Mar 23, 2023

Introduction

Palindrome number: If the reverse of a number is the same as the original number, it is called a palindrome number. For example: 121, 34566543, 6, 56765, 87678, 909 etc.

This article will discuss the approach to check palindrome number in c language.

Also see, Binary to Hex Converter.

Problem statement

We are given a number. Our task is to find whether the number is palindrome or not We will be specifically looking to check if the given number is a palindrome number in C language from the implementation perspective. If the given number is a palindrome, print "YES" else "NO".

Sample Test Cases

Example 1:

``````Input: 121
Output: YES

Explanation: After reversing 121, the reversed number is 121, so it is a palindrome number.``````

Example 2:

``````Input: 345676
Output: NO

Explanation: After reversing 345676, the reversed number is 676543, so it is not a palindrome number.``````

Approach

We will use the property of the palindrome number to solve this problem(Palindrome number in C). If we reverse a palindrome number, it remains the same as the original number.

The idea is simple; we reverse the given number. If the reversed number is the same as the original number, it is a palindrome number.

Steps:

• Store the given number in a variable named original.
• Reverse the value of the rev variable.
• If the given number is equal to the rev variable, print "YES".
• Else print "NO".

Let’s understand the above approach with an example: Given number = 121

• Store the given number in the original variable, original = 121.
• Declare a variable rev = 0.
• Now reverse the given number and store the result in the rev variable.
• We take the last digit from the given number and place it in the rev variable using the "%" operator for reversing the given number. We cut off the last taken digit from the given number using the "/" operator and repeat this whole process till the given number is not equal to 0.
• Now, rev=121 and original=121.
• So it is a palindrome number, print YES.

Let us now have a look at the implementation for the the problem: To check a palindrome number in C.

Implementation in C

``````#include<stdio.h>
int main()
{
int n = 121, rev = 0, rem, original;
original = n;
while (n != 0)
{
rem = n % 10;
rev = rev * 10 + rem;
n = n / 10;
}
if (original == rev)
printf("YES");
else
printf("NO");
}``````

Output:

``YES``

You can try it by yourself with the help of an online editor.

Complexity Analysis

Time complexity- The time complexity of the above approach is O(d), where d is the number of digits in the given number.

Space complexity - The space complexity of the above solution is O(1).

FAQs

1. Is it true that all palindromes are divisible by 11?
No, but palindromes with even digits are all divisible by 11.

2. What makes palindromic numbers unique?
A palindromic number (also called a numeral palindrome or a numeric palindrome) is a number that remains the same when its digits are reversed (for example, 16461).

3. Can a negative number be a palindrome?
An integer x is a palindrome if reverse(x) = x, where reverse(x) is x with its digits reversed. So, negative numbers are not palindromic.

Key Takeaways

In this article, we discussed the approach to check the palindrome number in c language with an example for a better understanding.

We hope that this blog has helped you enhance your knowledge regarding DSA and if you would like to learn more, check out our articles on link. Do upvote our blog to help other ninjas grow.

Check out this problem - Reverse Nodes In K Group

Happy Coding!