Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»C Programming»C Program to Check Palindrome Number

    C Program to Check Palindrome Number

    By RahulJune 24, 20233 Mins Read

    In the world of programming, palindrome numbers play a significant role due to their unique properties. A palindrome number is a number that remains the same when its digits are reversed. For example, 121, 3443, and 1221 are palindrome numbers. In this article, we will explore how to write a C program to check whether a given number is a palindrome or not. We will provide a step-by-step explanation along with an illustrative example.

    C Program to Check Palindrome Number

    Below is the C program that checks whether a given number is a palindrome or not.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    #include <stdio.h>
     
    int main() {
        int num, reversedNum = 0, originalNum, remainder;
        
        printf("Enter a positive integer: ");
        scanf("%d", &num);
        
        originalNum = num;
        
        // Reversing the number
        while (num != 0) {
            remainder = num % 10;
            reversedNum = reversedNum * 10 + remainder;
            num /= 10;
        }
        
        // Checking if the reversed number is equal to the original number
        if (originalNum == reversedNum)
            printf("%d is a palindrome.", originalNum);
        else
            printf("%d is not a palindrome.", originalNum);
        
        return 0;
    }

    Explanation:

    1. The program begins by declaring the necessary variables num, reversedNum, originalNum, and remainder.
    2. The user is prompted to enter a positive integer using the printf and scanf functions.
    3. The original value of the entered number is stored in the originalNum variable for later comparison.
    4. The next step involves reversing the number using a while loop. Inside the loop, the last digit of the number (num % 10) is extracted using the modulus operator (%), and it is added to the reversedNum variable after shifting the existing digits one place to the left (reversedNum = reversedNum * 10 + remainder). Then, the last digit is removed from the original number by dividing it by 10 (num /= 10).
    5. Once the loop finishes, the reversed number is stored in the reversedNum variable.
    6. Finally, the program compares the originalNum with reversedNum to determine if they are equal. If they are equal, the number is a palindrome and an appropriate message is displayed. Otherwise, it is not a palindrome.

    Example:

    Let’s consider an example to illustrate the working of the program. Suppose we want to check whether the number 12321 is a palindrome or not. Here’s how the program execution would progress:

    1. The user is prompted to enter a positive integer.
    2. The value 12321 is entered.
    3. The program begins the process of reversing the number.
    4. In the first iteration of the loop, the remainder is calculated as 12321 % 10, which results in 1. The reversedNum becomes 1, and num becomes 1232 (12321 / 10).
    5. In the second iteration, the remainder is 1232 % 10, resulting in 2. reversedNum becomes 12, and num becomes 123 (1232 / 10).
    6. The process continues until the num variable becomes 0.
    7. After completing the loop, the reversedNum variable holds the value 12321.
    8. The program then compares originalNum (12321) with reversedNum (12321).
    9. Since both numbers are equal, the program displays the message “12321 is a palindrome”.

    Conclusion

    The C program presented in this article allows us to determine whether a given number is a palindrome or not. By understanding the logic and structure of this program, programmers can build upon it to create more complex applications involving palindrome numbers. Palindrome numbers provide an interesting way to explore and practice various programming concepts.

    Palindrome Number
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    C Program to Reverse a String

    C Program to Reverse a Number

    C Program to Check if a Number is Odd or Even

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • How to Create and Use Custom Python Module
    • How to Install and Use Podman on Ubuntu 22.04 & 20.04
    • Setting Up Laravel with Docker and Docker-compose
    • Setting Up Development Environments with PHP and Docker
    • Using Composer with Different PHP Versions in Linux
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.