fastread homefastrread library fastread menu

C : Check Whether a Number is Palindrome or Not

Tutorial by:Manisha Dubey      Date: 2016-06-17 03:40:20

❰ Previous Next ❱

This program reverses an integer (entered by the user) using while loop. Then the if...else statement is used to check whether the number entered is equal to the reversed number.

An integer is a palindrome if the reverse of that number is equal to the original number.

Example: Program to Check Palindrome

#include <stdio.h>
int main()
{
    int n, reversedInteger = 0, remainder, originalInteger;

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

    originalInteger = n;

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

    // palindrome if orignalInteger and reversedInteger is equal
    if(originalInteger == reversedInteger)
        printf("%d is a palindrome.", originalInteger);
    else
        printf("%d is not a palindrome.", originalInteger);
    
    return 0;
}

Output

Enter an integer: 1001
1001 is a palindrome.

❰ Previous Next ❱


C

Submit Your Thought, Tutorial, Articls etc.

Submit Your Information India's Number one online promotion website