• This topic is empty.
  • C Program to check whether the given number is a Palindrome number or not?

    codewithc
    CWC Keymaster

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

    Palindrome Number is the number that is read from the same side when written down. The term Palindrome means the same backward as forward. For example, the number 98 is a Palindrome number because the digit of the number will be read from the same side as the digit is written. So, 98 is a Palindrome number.

    There are various programs available online which will give you the solution of the given palindrome number but the main question is how to check whether the given number is a palindrome number or not.

    Here I am going to share with you a C program that will give you the solution of the given palindrome number.

    In this program, we have included a function that will check whether the given number is a palindrome number or not.

    The source code of the program is given below:

    [dm_code_snippet background="no" background-mobile="no" slim="yes" bg-color="#abb8c3" theme="dark" language="clike" wrapped="no" height="" copy-text="Copy Code" copy-confirmed="Copied"]

    int isPalindrome(int num)
    {
    int a = 0, b = 0, i, j, k;
    a = num % 10;
    b = num / 10;
    for (i = a; i < a + 9; i++)
    {
    if (num == i)
    return 1;
    else
    num = num - i;
    }
    for (j = b; j >= a; j--)
    {
    if (num == j)
    return 1;
    else
    num = num + j;
    }
    return 0;
    }

    [/dm_code_snippet]

    [dm_code_snippet background="no" background-mobile="no" slim="yes" bg-color="#abb8c3" theme="dark" language="clike" wrapped="no" height="" copy-text="Copy Code" copy-confirmed="Copied"]

    int main()
    {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    printf("Is the given number is a palindrome number? ");
    if (isPalindrome(num))
    printf("Yes, it is a palindrome number.");
    else
    printf("No, it is not a palindrome number.");
    printf("\n");
    return 0;
    }

    [/dm_code_snippet]

    To check if a number is a palindrome, we’ll reverse the digits of the number and compare the reversed number with the original one. If they’re the same, voila, we’ve got ourselves a palindrome!

    Here’s the C code to do just that:

    [dm_code_snippet background="yes" background-mobile="yes" slim="no" line-numbers="no" bg-color="#abb8c3" theme="dark" language="php" wrapped="no" height="" copy-text="Copy Code" copy-confirmed="Copied"]

    #include <stdio.h>
    
    int main() {
        int num, reversed = 0, original, remainder;
    
        printf("Enter an integer: ");
        scanf("%d", &num);
    
        // Store the original number to compare later
        original = num;
    
        // Reverse the number
        while (num != 0) {
            remainder = num % 10;
            reversed = reversed * 10 + remainder;
            num /= 10;
        }
    
        // Check for palindrome
        if (original == reversed) {
            printf("%d is a palindrome.\n", original);
        } else {
            printf("%d is not a palindrome.\n", original);
        }
    
        return 0;
    }
    

    [/dm_code_snippet]

    How It Works

    1. Input the Number: We first take the number from the user.
    2. Store the Original: We store the original number in a separate variable, so we can compare it later.
    3. Reverse the Digits: We use a while loop to reverse the digits of the number. We get the last digit with num % 10, add it to the reversed number after shifting its digits, and then remove the last digit from the original number.
    4. Check & Display: Finally, we check if the reversed number is the same as the original number. If yes, it’s a palindrome.

    The real beauty of this problem is its simplicity. It’s a straightforward task, but it gives you a good handle on loops, conditionals, and basic arithmetic operations in C. Once you get the hang of it, you can even extend this logic to check for palindrome strings, not just numbers!

    So there you go—a neat little C program to find out if a given number is a palindrome or not. It’s like a mini-adventure in the world of programming. Thanks for coding along, and keep on crunching those numbers! ??

Viewing 0 reply threads
  • You must be logged in to reply to this topic.
en_USEnglish