C Program to Check Palindrome Number

In C programming, a palindrome number is a number that remains the same when its digits are reversed. For example, 121, 12321, and 123321 are all palindrome numbers. This concept is widely used in coding interviews and competitive programming. This article will explore three different methods to write C Program to Check Palindrome Number, along with detailed explanations, code examples, and outputs. You can also learn about how to write a c program to check Armstrong Number.

C Program to Check Whether a Number is Palindrome or Not

What is a Palindrome Number in C?

A palindrome number in C is a number that reads the same backward as forward. It is essential to understand this concept to write efficient C programs that can determine whether a given number is a palindrome. The basic idea is to reverse the digits of the number and compare the reversed number with the original.

Method 1) Checking Palindrome Number Using a While Loop

Code Example

#include <stdio.h>

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

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

    originalNum = num;

    while (num != 0) {
        remainder = num % 10;
        reversedNum = reversedNum * 10 + remainder;
        num /= 10;
    }

    if (originalNum == reversedNum) {
        printf("%d is a palindrome number.\n", originalNum);
    } else {
        printf("%d is not a palindrome number.\n", originalNum);
    }

    return 0;
}

Explanation

  1. Input and Initialization: The program takes an integer input from the user and stores it in the variable num. It then initializes reversedNum to 0.
  2. Reversing the Number: The while loop iterates through the digits of the number, reversing them by appending each digit to reversedNum.
  3. Comparison: After reversing, the program compares originalNum with reversedNum. If they are equal, the number is a palindrome; otherwise, it is not.

Output

Enter an integer: 123321
123321 is a palindrome number.

Method 2) Checking Palindrome Number Using Recursion

Code Example

#include <stdio.h>

int reverse(int num, int rev);

int main() {
    int num;

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

    if (num == reverse(num, 0)) {
        printf("%d is a palindrome number.\n", num);
    } else {
        printf("%d is not a palindrome number.\n", num);
    }

    return 0;
}

int reverse(int num, int rev) {
    if (num == 0) {
        return rev;
    }
    return reverse(num / 10, rev * 10 + num % 10);
}

Explanation

  1. Input and Initialization: The user enters an integer, which is passed to the reverse function along with an initial rev value of 0.
  2. Recursive Reversal: The reverse function recursively reverses the number by continuously dividing it by 10 and appending the remainder to rev.
  3. Comparison: After the reversal, the program compares the reversed number with the original. If they match, the number is a palindrome.

Output

Enter an integer: 1221
1221 is a palindrome number.

Method 3) Checking Palindrome Number Using String Conversion

Code Example

#include <stdio.h>
#include <string.h>

int main() {
    char numStr[100], reversedStr[100];
    int length, i;

    printf("Enter an integer: ");
    scanf("%s", numStr);

    length = strlen(numStr);

    for (i = 0; i < length; i++) {
        reversedStr[i] = numStr[length - i - 1];
    }
    reversedStr[length] = '\0';

    if (strcmp(numStr, reversedStr) == 0) {
        printf("%s is a palindrome number.\n", numStr);
    } else {
        printf("%s is not a palindrome number.\n", numStr);
    }

    return 0;
}

Explanation

  1. Input as String: The number is taken as a string input, which allows easy manipulation of individual digits.
  2. String Reversal: A for loop reverses the string by iterating through each character from the end to the beginning.
  3. Comparison: The program compares the original string with the reversed string using strcmp. If they are identical, the number is a palindrome.

Output

Enter an integer: 12321
12321 is a palindrome number.

FAQs About Palindrome Numbers in C

1) How do you check if a number is a palindrome in C?

To check if a number is a palindrome in C, reverse the digits of the number and compare the reversed number with the original number. If both are identical, the number is a palindrome. You can achieve this using loops, recursion, or string manipulation.

2) How to check if a number is a palindrome?

To check if a number is a palindrome, follow these steps:

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

3) How do you write a program to check palindrome?

A simple C program to check if a number is a palindrome involves reversing the digits of the input number and comparing it with the original. Here’s an example:

#include <stdio.h>

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

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

    originalNum = num;

    while (num != 0) {
        remainder = num % 10;
        reversedNum = reversedNum * 10 + remainder;
        num /= 10;
    }

    if (originalNum == reversedNum) {
        printf("%d is a palindrome.\n", originalNum);
    } else {
        printf("%d is not a palindrome.\n", originalNum);
    }

    return 0;
}

4) Is 121 a palindrome number?

Yes, 121 is a palindrome number because it reads the same forward and backward.

5) How to find palindrome words in C?

To find palindrome words in C, treat the input word as a string, reverse the string, and compare the reversed string with the original. If both are the same, the word is a palindrome.

6) What is a palindrome of 89?

The palindrome of 89 would be 98. However, 89 itself is not a palindrome because it does not read the same forward and backward.

7) What is the formula for a palindrome number?

There is no specific “formula” for a palindrome number. The general method involves reversing the number’s digits and checking if the reversed number is equal to the original number.

8) What is palindrome number code?

Palindrome number code refers to a program written to check whether a given number is a palindrome. You can write such a program using a while loop, recursion, or string manipulation to reverse the number and compare it with the original.

9) What is an example of a palindrome?

An example of a palindrome number is 12321. It reads the same forward and backward.

10) How to check palindrome using a queue in C?

To check a palindrome using a queue in C, you can enqueue the digits of the number and simultaneously dequeue them from the rear and compare them to the front. If all the corresponding digits match, the number is a palindrome.

11) Are all palindromes divisible by 11?

No, not all palindromes are divisible by 11. Some palindrome numbers like 121 or 1331 are divisible by 11, but there are many palindrome numbers, like 12321, that are not divisible by 11.

12) How do you write a palindrome?

To write a palindrome number, ensure that the digits of the number are the same when reversed. For example, 1221, 34543, and 12321 are palindromes.

13) Is 11 11 11 a palindrome?

Yes, the number 111111 is a palindrome because it reads the same forward and backward.

14) How do you identify a palindrome number?

To identify a palindrome number, reverse the digits of the number and compare it with the original number. If both are the same, the number is a palindrome.

15) Which number is a 6-digit palindrome?

A common example of a 6-digit palindrome number is 123321. It reads the same backward as forward. Other examples include 456654 and 789987.

Categories C

Leave a Comment