Beginner Number Problems C

Palindrome Number Program in C with Output

Check whether an integer reads the same forwards and backwards in C by reversing its digits and comparing with the original.

  • TimeO(d),
  • SpaceO(1)
  • Hints3
  • Read3 min
Quick answer

A palindrome number reads the same forwards and backwards, like 121 or 1001. In C, save the number in original, then build the reverse with a while loop: reversed = reversed * 10 + n % 10; n = n / 10;. When n becomes 0, compare original with reversed. If they are equal, it is a palindrome. 121 is a palindrome, 123 is not.

Digit extraction with % and /while loopFunctionsInteger overflow
1

Problem statement

Write a palindrome number program in C. Read an integer and print whether it is a palindrome.

Use the reverse-and-compare method: reverse the digits with arithmetic, then compare the reverse with the original number. Show the output for 121 and 123.

Input

A single integer n.

Output

"<n> is a palindrome number" or "<n> is not a palindrome number".

Examples

Example 1
Inputn = 121
Output121 is a palindrome number

The reverse of 121 is 121, which equals the original.

Example 2
Inputn = 123
Output123 is not a palindrome number

The reverse of 123 is 321, which is different.

Example 3
Inputn = 1001
Output1001 is a palindrome number

The reverse of 1001 is 1001.

Constraints

  • n fits in a 32-bit int
  • Negative numbers are treated as not palindromes
2

Think before you code

Reveal the questions to ask yourself first
  • How do you get the last digit of n, and how do you remove it?
  • The loop changes n to 0. What do you compare with at the end?
  • Can the reverse of a valid int be too large for an int?
3

Hints

Open one at a time — try again after each.
1 Hint 1
n % 10 gives the last digit, and n / 10 removes it (integer division in C).
2 Hint 2
Copy n into original before the loop, because the loop destroys n.
3 Hint 3
Build the reverse with reversed = reversed * 10 + digit.
4

Approach

Reveal the step-by-step approach

Palindrome logic in C: reverse and compare

  1. Read n and copy it: original = n.
  2. Set reversed = 0.
  3. While n > 0:
    • digit = n % 10
    • reversed = reversed * 10 + digit
    • n = n / 10
  4. If original == reversed, the number is a palindrome.

In C, dividing two int values with / drops the decimal part, so 121 / 10 is 12. That is exactly what removes the last digit.

n Reverse Palindrome?
121 121 Yes
123 321 No
1001 1001 Yes
10 1 No
5

Dry run

Walk through the example step by step

n = 121, original = 121, reversed = 0:

n   | digit = n % 10 | reversed = reversed * 10 + digit | n / 10
----+----------------+----------------------------------+-------
121 | 1              | 1                                | 12
12  | 2              | 12                               | 1
1   | 1              | 121                              | 0

original (121) == reversed (121), so 121 is a palindrome.

For n = 123 the same steps give reversed = 321. 123 != 321, so it is not.

6

Solution

Reveal the full C solution
solution.c
#include <stdio.h>

int main(void) {
    int n, original, reversed = 0, digit;

    printf("Enter a number: ");
    scanf("%d", &n);

    original = n;
    while (n > 0) {
        digit = n % 10;
        reversed = reversed * 10 + digit;
        n = n / 10;
    }

    if (original == reversed) {
        printf("%d is a palindrome number\n", original);
    } else {
        printf("%d is not a palindrome number\n", original);
    }
    return 0;
}

Output (two separate runs):

Enter a number: 121
121 is a palindrome number
Enter a number: 123
123 is not a palindrome number

The program prints original, not n, because n is 0 after the loop.

Check palindrome in C with a function

A function makes the check reusable. Here the reverse is stored in long long, so it cannot overflow even for the largest int values.

#include <stdio.h>

int isPalindrome(int n) {
    if (n < 0) {
        return 0;
    }
    long long reversed = 0;
    int temp = n;
    while (temp > 0) {
        reversed = reversed * 10 + temp % 10;
        temp /= 10;
    }
    return reversed == n;
}

int main(void) {
    int tests[] = {121, 123, 0, 1001, 10, -121, 2147447412};
    int count = sizeof(tests) / sizeof(tests[0]);

    for (int i = 0; i < count; i++) {
        printf("%d -> %s\n", tests[i], isPalindrome(tests[i]) ? "palindrome" : "not palindrome");
    }

    printf("Palindromes from 100 to 200: ");
    for (int n = 100; n <= 200; n++) {
        if (isPalindrome(n)) {
            printf("%d ", n);
        }
    }
    printf("\n");
    return 0;
}
121 -> palindrome
123 -> not palindrome
0 -> palindrome
1001 -> palindrome
10 -> not palindrome
-121 -> not palindrome
2147447412 -> palindrome
Palindromes from 100 to 200: 101 111 121 131 141 151 161 171 181 191

Why the reverse can overflow

The largest 32-bit int is 2147483647. A number like 1000000009 fits, but its reverse 9000000001 does not. With an int reverse, signed overflow is undefined behaviour in C, so the result cannot be trusted. Storing the reverse in long long, as in the function above, avoids this.

Palindrome check with a string

If the number is read as text, you can compare characters from both ends without any arithmetic.

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

int main(void) {
    char s[20];
    printf("Enter a number: ");
    scanf("%19s", s);

    int len = strlen(s);
    int isPal = 1;
    for (int i = 0; i < len / 2; i++) {
        if (s[i] != s[len - 1 - i]) {
            isPal = 0;
            break;
        }
    }
    printf("%s\n", isPal ? "Palindrome" : "Not a palindrome");
    return 0;
}
Enter a number: 12321
Palindrome

Most lab questions ask for the arithmetic method, so learn that one first.

The Java version is check whether a number is a palindrome, and the Python version for numbers and strings is the palindrome program in Python. More lab programs are in C programs for beginners.

TimeO(d), where d is the number of digits
SpaceO(1)
7

Review before the interview

Common mistakes

  • Comparing reversed with n after the loop. n is 0 by then; compare with the saved original.
  • Not initialising reversed to 0. A local int starts with garbage in C, so the reverse comes out wrong.
  • Using an int for the reverse of large inputs such as 1000000009. The reverse overflows; use long long.
  • Writing reversed = digit * 10 + reversed. That adds digits in the wrong place and gives the original order.

Edge cases to test

  • 0 is a palindrome: the loop does not run, reversed stays 0, and 0 == 0.
  • Numbers ending in 0, like 10 or 120, are never palindromes (except 0).
  • Negative numbers such as -121 are treated as not palindromes because of the minus sign.
  • Single-digit numbers 1 to 9 are always palindromes.

Interview follow-ups

  • How would you check a palindrome by reversing only half of the digits?
  • How would you check whether a string, not a number, is a palindrome in C?
  • How would you print all palindrome numbers between 1 and 1000?

Keep practising

Building your programming basics?

CodeBegun’s programs build problem-solving with mentor review and mock interviews, online or in Madhapur, Hyderabad.

Chat with us