Q:

C program to check whether number is Palindrome or not

0

C program to check whether number is Palindrome or not

Palindrome Number - The number which is equal to reverse number know as Palindrome Number. For example Number 12321 is a Palindrome Number, because 12321 is equal to it’s reverse Number 12321.

In this program, we will read an integer number and check whether it is Palindrome Number or not, to check Palindrome Number firstly we will calculate it’s Reverse Number. If input Number is equal to Reverse Number then Number will be a Palindrome Number.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Check Palindrome Number using C program

/* C program to check whether a number is palindrome or not */

#include <stdio.h>

int main()
{
    int number, revNumber = 0, rem = 0, tempNumber;

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

    tempNumber = number;

    while (tempNumber != 0) {
        rem = tempNumber % 10;
        revNumber = revNumber * 10 + rem;
        tempNumber /= 10;
    }

    /* checking if number is equal to reverse number */
    if (revNumber == number)
        printf("%d is a palindrome.", number);
    else
        printf("%d is not a palindrome.", number);

    return 0;
}

Using User Define Function

/* C program to check whether a number is palindrome or not */

#include <stdio.h>

/*function to check Palindrome Number*/
int isPalindrome(int num)
{
    int tempNumber = num;
    int dig, revNumber;

    /*getting reverse number*/
    revNumber = 0;
    while (num > 0) {
        dig = num % 10;
        revNumber = (revNumber * 10) + dig;
        num /= 10;
    }

    if (revNumber == tempNumber)
        return 1; /*Palindrome Number*/
    else
        return 0; /*Not a Palindrome Number*/
}

int main()
{
    int number;

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

    if (isPalindrome(number))
        printf("%d is a palindrome.", number);
    else
        printf("%d is not a palindrome.", number);

    return 0;
}

Output:

    First run:
    Enter an integer number: 12321
    12321 is a palindrome.

    Second run:
    Enter an integer number: 1234
    1234 is not a palindrome.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now