Table of contents
1.
Introduction
2.
What is Palindrome Number in C?
3.
Palindrome Number Algorithm
4.
Different Ways to Check for Palindrome Number in C
4.1.
Using a Loop:
4.2.
Using Recursion:
4.3.
Using String Manipulation:
4.4.
Using an Array:
5.
Another Different Ways to Check for Palindrome Number in C
5.1.
1. By Reversing the Number
5.2.
C
5.3.
2. Using Two Pointers After String Conversion
5.4.
C
6.
Problem statement
7.
Sample Test Cases
7.1.
Example 1
7.2.
Example 2
8.
Approach
9.
Implementation of Palindrome Number Program in C 
9.1.
C
10.
Complexity Analysis
11.
Frequently Asked Questions
11.1.
Is it true that all palindromes are divisible by 11?
11.2.
What makes palindromic numbers unique?
11.3.
How to find longest palindrome in a string in C?
11.4.
Can a negative number be a palindrome?
12.
Conclusion
Last Updated: Dec 14, 2024
Easy

Palindrome Number in C

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this article, we will explore palindrome numbers in C programming. A palindrome number is a number that remains the same when its digits are reversed. We will discuss its definition, present an algorithm to determine if a number is a palindrome, and explore different methods to check for palindrome numbers in C.

Palindrome Number in C

What is Palindrome Number in C?

A palindrome number in C is a number that reads the same backward as forward. For example, numbers like 121, 1331, and 12321 are considered palindromes because they remain unchanged when their digits are reversed. Understanding palindrome numbers is essential for various applications in programming, such as validating inputs and solving certain algorithmic problems.

Palindrome Number Algorithm

  1. Start the program.
     
  2. Initialize a variable to hold the original number.
     
  3. Initialize a variable to hold the reversed number as 0.
     
  4. Loop until the original number becomes 0:
    • Extract the last digit using the modulus operator (%).
       
    • Add this digit to the reversed number by multiplying the reversed number by 10 and adding the extracted digit.
       
    • Remove the last digit from the original number by dividing it by 10.
       
  5. Compare the original number with the reversed number.
     
  6. If they are the same, print that the number is a palindrome; otherwise, print that it is not.
     
  7. End the program.

Different Ways to Check for Palindrome Number in C

There are several methods to check for palindrome numbers in C:

Using a Loop:

  • Reverse the number using a loop and compare it with the original number.
     

Using Recursion:

  • Implement a recursive function that compares the digits from the beginning and the end of the number.
     

Using String Manipulation:

  • Convert the number to a string and check if the string is equal to its reverse.
     

Using an Array:

  • Store the digits in an array and then check if the elements from both ends are equal.
     

Each method has its advantages, depending on the specific requirements and constraints of the program.

Another Different Ways to Check for Palindrome Number in C

1. By Reversing the Number

This method involves reversing the digits of the number and comparing it to the original number. If both the reversed number and the original number are the same, the number is a palindrome.

Example:

  • C

C

#include <stdio.h>

int main() {
int num, reversed = 0, remainder, original;

printf("Enter an integer: ");
scanf("%d", &num);

original = num; // Store the original number

// Reverse the number
while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}

// Check if the original and reversed numbers are equal
if (original == reversed)
printf("%d is a palindrome number.\n", original);
else
printf("%d is not a palindrome number.\n", original);

return 0;
}
You can also try this code with Online C Compiler
Run Code

Output

Enter an integer: 121 
121 is a palindrome number.

2. Using Two Pointers After String Conversion

This method involves converting the number into a string and then using two pointers to check if the string is a palindrome. One pointer starts at the beginning of the string, and the other starts at the end, comparing corresponding characters until they meet in the middle.

Example:

  • C

C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);

// Convert the number to a string
char str[20];
sprintf(str, "%d", num);

int left = 0;
int right = strlen(str) - 1;

// Use two pointers to check if the string is a palindrome
while (left < right) {
if (str[left] != str[right]) {
printf("%d is not a palindrome number.\n", num);
return 0;
}
left++;
right--;
}

printf("%d is a palindrome number.\n", num);
return 0;
}
You can also try this code with Online C Compiler
Run Code

In this method, the number is converted to a string using sprintf, and two pointers are used to compare characters from both ends of the string. If any characters don't match, the number is not a palindrome.

Output

Enter an integer: 123
123 is not a palindrome number.

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 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");
}
You can also try this code with Online C Compiler
Run Code

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

You can also read about Palindrome Number in Python here.

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

In this article, we have examined the concept of palindrome numbers in C, which is essential for understanding numeric properties and algorithms. We learned what a palindrome number is, along with its significance and applications.

Check out this problem - Reverse Nodes In K Group

Live masterclass