Write a Program to Check if a Number is Palindrome or Not in C

Write a Program to Check if a Number is Palindrome or Not

A palindrome is a number (or a word) that reads the same backward as forward. For example, the numbers 121, 1331, and 98789 are palindromes, while 123 and 456 are not. Checking if a number is a palindrome is a popular problem in programming, and it helps improve your understanding of loops, conditionals, and number manipulation. In this blog post, we will show you how to write a C program to check if a given number is a palindrome or not, and explain the logic behind it.

What is a Palindrome?

A palindrome is a number or a sequence that remains the same when its digits or characters are reversed. For example:

  • Palindrome Numbers: 121, 12321, 111 are palindromes because reversing their digits gives the same number.
  • Non-Palindrome Numbers: 123, 456, 789 are not palindromes because their digits change when reversed.

The task is simple: take a number, reverse its digits, and check whether the reversed number is equal to the original number.

1. Logic to Check if a Number is Palindrome

The logic to check if a number is a palindrome is similar to reversing a number. The steps are as follows:

  1. Read the number from the user.
  2. Reverse the digits of the number using a loop.
  3. Compare the reversed number with the original number.
  4. If they are equal, the number is a palindrome; otherwise, it is not.

2. Writing the Program to Check for Palindrome

Now that we understand the logic, let's write the program in C.

Code Example: Palindrome Check in C

#include <stdio.h>

int main() {
    int num, originalNum, reversed = 0, remainder;

    // Ask the user to enter a number
    printf("Enter an integer: ");
    scanf("%d", &num);

    // Store the original number for comparison later
    originalNum = num;

    // Reverse the number
    while (num != 0) {
        remainder = num % 10;  // Get the last digit
        reversed = reversed * 10 + remainder;  // Build the reversed number
        num /= 10;  // Remove the last digit from the original number
    }

    // Check if the original number is equal to the reversed number
    if (originalNum == reversed) {
        printf("%d is a palindrome.\n", originalNum);
    } else {
        printf("%d is not a palindrome.\n", originalNum);
    }

    return 0;
}

Explanation of the Program

Here's a breakdown of how the program works:

  • Variables: We declare the following variables:
    • num: the input number from the user.
    • originalNum: stores the original number for comparison.
    • reversed: stores the reversed version of the number.
    • remainder: holds the last digit of the number during each iteration of the loop.
  • Loop: The while loop runs as long as num is not zero. For each iteration:
    • The last digit of num is extracted using the modulus operator (%).
    • The extracted digit is added to reversed, constructing the reversed number.
    • num is divided by 10 to remove the last digit.
  • Comparison: After the loop, the original number (originalNum) is compared with the reversed number. If they are the same, the number is a palindrome.

3. Example Output

Here’s an example of how the program works:

Enter an integer: 121
121 is a palindrome.

In this example, the user inputs the number 121, and the program confirms that it is a palindrome.

4. Handling Negative Numbers

Our current program works only for positive numbers. If the user enters a negative number, the program will not check it correctly. For example, -121 would be reversed to -121, but we don't typically consider negative numbers as palindromes. Let’s modify the program to ignore negative numbers, or you can treat negative numbers based on your specific use case.

Code Example: Handle Negative Numbers

#include <stdio.h>

int main() {
    int num, originalNum, reversed = 0, remainder;

    // Ask the user to enter a number
    printf("Enter an integer: ");
    scanf("%d", &num);

    // Check if the number is negative
    if (num < 0) {
        printf("Negative numbers cannot be palindromes.\n");
        return 0;
    }

    originalNum = num;

    // Reverse the number
    while (num != 0) {
        remainder = num % 10;
        reversed = reversed * 10 + remainder;
        num /= 10;
    }

    // Check if the original number is equal to the reversed number
    if (originalNum == reversed) {
        printf("%d is a palindrome.\n", originalNum);
    } else {
        printf("%d is not a palindrome.\n", originalNum);
    }

    return 0;
}

Explanation of Changes

The updated version of the program includes a check for negative numbers. If the number is negative, we simply print a message and exit the program. This avoids reversing negative numbers since, by definition, they are not palindromes.

5. Common Mistakes to Avoid

  • Incorrect Handling of Negative Numbers: Be mindful of how you handle negative numbers. Typically, negative numbers are not considered palindromes, but you can modify the program to suit your requirements.
  • Overflow: If you’re working with very large numbers, be cautious of integer overflow when reversing the number. For most typical problems, standard integer data types will work fine.
  • Zero as Input: The number 0 is a valid palindrome since it remains the same when reversed. Ensure your program correctly handles this edge case.

6. Expanding the Program

After mastering the basics of checking if a number is a palindrome, here are some ways you can enhance the program:

  • Extend the program to check if a string is a palindrome.
  • Modify the program to handle larger numbers using arrays or strings to avoid overflow issues.
  • Combine the palindrome check with other conditions, such as checking for prime palindromes.

7. Conclusion

In this blog post, we’ve walked through how to write a C program to check if a number is a palindrome. We explored the logic of reversing a number and comparing it with the original number, handled negative inputs, and discussed common mistakes to avoid. Checking for palindromes is a simple yet powerful problem that builds your understanding of loops, conditionals, and number manipulation.

Now that you’ve learned how to check for palindromes, try experimenting with more advanced problems and exploring other uses for palindromes in programming challenges. Happy coding!

0 Comments

Post a Comment

Post a Comment (0)

Previous Post Next Post