Open In App

Palindrome Number Program in C

Last Updated : 16 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Write a C program to check whether a given number is a palindrome or not. Palindrome numbers are those numbers which after reversing the digits equals the original number.

Examples

Input: 121
Output: Yes
Explanation: The number 121 remains the same when its digits are reversed.

Input: 123
Output: No
Explanation: The number 123 does not remain the same when its digits are reversed.

Different Ways to Check for Palindrome Number in C

In C, we can check whether the given number is palindrome or not using two methods given below:

1. By Reversing the Number

A simple method for this problem is to first reverse all the digits of a given number using arithmetic operations and then compare the reverse of the number with a given number. If both are the same, then return true, else return false.

Implementation

C
// C Program to check if a number is a palindrome by
// reversing the number
#include <stdio.h>

int reverseNum(int N) {
  
      // Function to store the reversed number
    int rev = 0;
    while (N > 0) {

        // Extract the last digit
        int dig = N % 10;

        // Append the digit to the reversed number
        rev = rev * 10 + dig;

        // Remove the last digit
        N /= 10;
    }
    return rev;
}

int isPalindrome(int N) {
  
    // Negative numbers are not palindromes
    if (N < 0)
        return 0;
    return N == reverseNum(N);
}

int main() {
    int N = 121;
    if (isPalindrome(N)) {
        printf("Yes\n");
    }
    else {
        printf("No\n");
    }
    return 0;
}

Output
Yes

Time Complexity: O(logN) where N is the number.
Auxiliary Space: O(1)

2. Using Two Pointers After String Conversion

In two-pointer technique, first convert the number into a string and then take two pointers: one pointing at the start of the string and the other at the end of the string. Compare the characters they point to while moving these pointers towards each other. If all the characters match by the time pointers meets, the number is a palindrome, otherwise it is not.

Implementation

C
// C Program to check if a number is a palindrome by
// converting it to a string and using two pointers
#include <stdio.h>
#include <string.h>

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

      // Left pointer starting from the first character
    int left = 0;   
  
      // Right pointer starting from the last character
    int right = strlen(str) - 1;

    // Loop until the pointers meet in the middle
    while (left < right) {
      
        // If mismatch is found (not a palindrome)
        if (str[left] != str[right]) {
            return 0;
        }
          
          // Move pointers towards each other
        left++;
        right--;
    }

    return 1;
}

int main() {
    int num = 1221;

    // Check if the number is a palindrome and print the result
    if (isPalindrome(num)) {
        printf("Yes\n");
    }
    else {
        printf("No\n");
    }

    return 0;
}

Output
Yes

Time Complexity: O(logN), where N is the number.
Auxiliary Space: O(logN) due to the string conversion

Frequently Asked Questions – FAQs

Are negative numbers considered palindromes?

No, negative numbers are not considered palindromes because the negative sign will not match when the digits are reversed.

What is the most efficient method to check for a palindrome number?

The most efficient method in terms of space is reversing the number directly (Method 1), as it has O(1) auxiliary space complexity.

Can a single-digit number be a palindrome?

Yes, all single-digit numbers are palindromes since they read the same forward and backward.




Next Article

Similar Reads

three90RightbarBannerImg
  翻译: