×

C Programs

C Basic & Conditional Programs

C Looping Programs

C String Programs

C Miscellaneous Programs

C program to check whether number is Palindrome or not

What is 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.

Checking whether number is Palindrome or not

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.

C program to check whether number is Palindrome or not

/* 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;
}

Checking palindrome number using user-defined 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.

C Number Manipulation Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.