C Program to check whether the given number is a Palindrome or Not

In this C Programming example, we will implement the program to check whether the given number is a Palindrome or not by using the user’s input and print it on screen. We will implement the program using loops and recursive functions.

1. Palindrome number

A Palindrome is a word, string, number or any sequence that reads same forward and backward.

Palindromic number is a number that reads the same when the digits are read backward (such as 121 or 11211).

Helpful topics to understand this program better are


2. Palindrome program in C using Loop

Let’s discuss the execution(kind of pseudocode) for the program to find whether a string is a Palindrome or not in C.

  • In the first step of the program, the number(integer) is entered by the user and is stored in a temporary variable i.e. x.
  • This temporary variable is equal to the originalNumber.
  • The originalNumber is then reversed and is stored in the reversedNumber variable.
  • The next step is to compare both the variables i.e. originalNumber and reversedNumber.
  • If both numbers are the same, print it as a palindrome number. Else print not a palindrome number.

Let us implement this concept in the c program and check whether the given number is Palindrome or not.

#include <stdio.h>
int main() {
    int x, reversedNumber = 0, remainder, originalNumber;
    printf("Enter an integer: ");
    scanf("%d", &x);
    originalNumber = x;

    // reversed integer is stored in reversedNumber
    while (x != 0) {
        remainder = x % 10;
        reversedNumber = reversedNumber * 10 + remainder;
        x /= 10;
    }

    // condition is checked if the given number is palindrome or not.
    if (originalNumber == reversedNumber)
        printf("%d is a palindrome.", originalNumber);
    else
        printf("%d is not a palindrome.", originalNumber);

    return 0;
}
Output
Testing for integer...
Enter an integer: 121
121 is a palindrome.

3. Palindrome number program in C using Recursion

In the last section, we have implemented a program using loops, let’s now use the Recursive approach to validate if the given number is a Palindrome or not.

In this, we use recursion to find the reverse of a number and then compare the reversedNumber with the originalNumber to validate if the number is a Palindrome or not.

#include <stdio.h>
#include <math.h>

int reverse(int number);
int isPalindrome(int number);

int main()
{
    int number;

    //Input is entered by the user
    printf("Enter an Integer:");
    scanf("%d", &number);

    //Condition is verified for a number to be a palindrome or not
    if (isPalindrome(number) == 1)
    {
        printf("%d is a Palindrome number.\n", number);
    }
    else
    {
        printf("%d is not Palindrome number.\n", number);
    }
    return 0;
}

//Recursive function to find reverse of any number
int reverse(int number)
{
    //Finding the number of digits in numbers
    int digits = (int)log10(number);
    if (number == 0)
        return 0;

    return ((number % 10 * pow(10, digits)) + reverse(number / 10));
}

// Function to verify whether a number is a palindrome or not.
// This function returns 1 if the number is palindrome or else it will return 0.
int isPalindrome(int number)
{
    //Check if the given number is equal to its reverse.

    if (number == reverse(number))
    {
        return 1;
    }
    return 0;
}
Output
Testing for integer...
Enter an Integer:12321
12321 is a Palindrome number.

4. Conclusion

In this C Programming example, we have discussed how to check whether the given number is Palindrome or not via two different approaches, using a loop and using recursive functions, and finally, print the result to the console.


Helpful Links

Please follow C Programming tutorials for the complete tutorial series.

Also for the example C programs please refer to C Programming Examples.

All examples are hosted on Github.

An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding.
Do not forget to share and Subscribe.
Happy coding!! 😊

Recommended -

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x