Palindrome program in C

Palindrome program in C
Palindrome Program in C

Problem Statement: Given a number N, write a C program to check if the number is Palindrome or Not.

Examples:

Example 1:
Input: N = 242
Output: Palindrome Number
Explanation: The reverse of 242 is equal to original number.

Example 2:
Input: N = 142
Output: Not palindrome number
Explanation: The reverse of 142 is not eqaul to original number

What is Plaindrome?

A number which is exactly the same as its reverse is called a palindrome.

For Example:

111 is a palindrome, as it is same when reversed.

Also, 
121 is a palindrome.

Similarly, 
131
232
444
505
are palindrome

How to check if a number is palindrome?

From the definition since it is clear that a number is palindrome if it is the same when reversed. So, we simply need to find the reverse of a number and compare it with the original number.

Approach:

We need to reverse the original number and store it in a temporary variable and then check if the reversed number is equal to the original number then it is palindrome else, not a palindrome number

  • At first we will store the given number into an temporary variable.
  • We will reverse the temporary variable.
  • Now we will check if the reversed number is equal to the given original number.
  • If the numbers are equal we print Palindrome number else Not Palindrome.

Code:

C Program

#include <stdio.h>
int palindrome(int n){
    int n1=n,sum=0;
    while(n1!=0){
        int r=n1%10;
        sum=sum*10+r;
        n1/=10;
    }
    return n==sum;
}
int main() {
    int n=232;
    if(palindrome(n))
        printf("It is a palindrome number");
    else
        printf("It is not a plaindrome number");
    return 0;
}

Output: It is a palindrome number

Time Complexity: O(log n)

Space Complexity: O(1)

Special thanks to Subhrajit Das for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article