Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Problem statement
Sample Test Cases
1.
Example 1:
2.
Example 2:
Approach
1.
Implementation of Palindrome Number Program in C 
2.
C
Complexity Analysis
1.
Frequently Asked Questions
1.1.
Is it true that all palindromes are divisible by 11?
1.2.
What makes palindromic numbers unique?
1.3.
How to find longest palindrome in a string in C?
1.4.
Can a negative number be a palindrome?
2.
Conclusion
Last Updated: Jul 12, 2024
Easy

Palindrome Number in C

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

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.

Palindrome Number in C

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

Also see, Binary to Hex Converter and C Static Function.

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.

You can also read about dynamic array in cShort int in C Programming and Tribonacci Series

Read Also -   Strong number in c

Example 2:

Input: 345676
Output: NO

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


Must Read  C Program to Reverse a 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 of Palindrome Number Program in C 

  • C

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.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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).

You can also read about Palindrome Number in Python here.

Must Read what is storage class in c and Decision Making in C

Frequently Asked Questions

Is it true that all palindromes are divisible by 11?

No, but palindromes with even digits are all divisible by 11.

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).

How to find longest palindrome in a string in C?

To find the longest palindrome in a string in C, iterate through each character and expand around it to check for palindromes of odd and even lengths. Keep track of the longest palindrome found.

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.

Conclusion

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

Check out this problem - Reverse Nodes In K Group

Also read palindrome number in python., Palindrome string

Live masterclass