Palindrome Number in C (Program using Loop, Function, Recursion)

A palindrome number is one of the most frequently asked questions in C programming interviews, coding rounds, lab exams, and technical tests. Companies like TCS, Infosys, Wipro, Accenture, Cognizant, and many startups regularly ask this problem to test logic, loops, and number manipulation.

In this guide, you’ll learn palindrome number in C language with:

  • Simple explanation for beginners
  • Programs using while loop, for loop, do while loop
  • Palindrome number in C using function
  • Palindrome number in C using recursion
  • Algorithm, flowchart, dry run
  • Time & space complexity
  • Interview questions and answers

This is a complete interview-ready resource.

Palindrome Number in C Program

What is a Palindrome Number? (Interview Definition)

A palindrome number is a number that remains the same when its digits are reversed. In C, a palindrome number is checked by reversing the number using modulo (%) and division (/) operations and comparing it with the original number.

while(n>0){
rev = rev*10 + n%10;
n /= 10;
}

Examples

  • 121 → Palindrome
  • 1331 → Palindrome
  • 123 → Not a palindrome

Why Palindrome Number is Important for Interviews

Interviewers use this problem to check:

  • Loop understanding
  • Modulo and division logic
  • Function and recursion knowledge
  • Time complexity awareness

Interview Tip: Always explain the logic first, then write the code.

Algorithm to Check Palindrome Number in C

Steps to check palindrome number in C:

  1. Read the input number
  2. Store the number in a temporary variable
  3. Reverse the number digit by digit
  4. Compare the reversed number with the original number
  5. If both are equal → Palindrome
  6. Otherwise → Not a palindrome

Palindrome Number in C Using While Loop

This is the most common and interview-preferred approach.

#include <stdio.h>

int main() {
    int num, original, reverse = 0, remainder;

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

    original = num;

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

    if (original == reverse)
        printf("Palindrome number");
    else
        printf("Not a palindrome number");

    return 0;
}

Palindrome Number in C Using For Loop

#include <stdio.h>

int main() {
    int num, original, reverse = 0;

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

    original = num;

    for (; num != 0; num = num / 10) {
        reverse = reverse * 10 + num % 10;
    }

    if (original == reverse)
        printf("Palindrome number");
    else
        printf("Not a palindrome number");

    return 0;
}

Palindrome Number in C Using Do While Loop

#include <stdio.h>

int main() {
    int num, original, reverse = 0;

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

    original = num;

    do {
        reverse = reverse * 10 + num % 10;
        num = num / 10;
    } while (num != 0);

    if (original == reverse)
        printf("Palindrome number");
    else
        printf("Not a palindrome number");

    return 0;
}

Palindrome Number in C Using Function

Using functions improves modularity and code reusability, which interviewers like.

#include <stdio.h>

int isPalindrome(int num) {
    int original = num, reverse = 0;

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

    return original == reverse;
}

int main() {
    int num;

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

    if (isPalindrome(num))
        printf("Palindrome number");
    else
        printf("Not a palindrome number");

    return 0;
}

Palindrome Number in C Using Recursion (Interview Favorite)

This version is often asked as a follow-up interview question.

#include <stdio.h>

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

int main() {
    int num, reverse;

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

    reverse = reverseNumber(num, 0);

    if (num == reverse)
        printf("Palindrome number");
    else
        printf("Not a palindrome number");

    return 0;
}

C Program to Find Factorial using Recursion, Function, While Loop, Pointers, If-Else

Dry Run of Palindrome Number Program (Input: 121)

Step num remainder reverse
1 121 1 1
2 12 2 12
3 1 1 121

Original = 121
Reverse = 121
Palindrome number

Comparison of Approaches

Method Interview Value Difficulty
While loop ⭐⭐⭐⭐⭐ Easy
For loop ⭐⭐⭐⭐ Easy
Function ⭐⭐⭐⭐⭐ Easy
Recursion ⭐⭐⭐⭐⭐ Advanced

Flowchart of Palindrome Number in C

Flowchart Steps:

  1. Start
  2. Input number
  3. Reverse the number
  4. Compare original and reversed values
  5. Display result
  6. End

Flowchart of Palindrome Number in C

Time and Space Complexity

  • Time Complexity: O(log₁₀ n)
  • Space Complexity: O(1) (iterative methods)

Why?
Each loop iteration processes one digit.

Common Edge Cases (Interview Perspective)

  • Negative numbers → ❌ Not palindrome
  • Single digit numbers → ✅ Always palindrome
  • Numbers ending with 0 (except 0 itself) → ❌ Not palindrome

Palindrome Number in C – Interview Questions & Answers

What is a palindrome number?

A number that reads the same forward and backward.

How do you check palindrome number in C?

By reversing the number and comparing it with the original number.

Which loop is best for palindrome number in C?

The while loop is most commonly used and easiest to explain.

Can palindrome be checked without loops?

Yes, using recursion.

What is the time complexity of palindrome program in C?

O(log₁₀ n).

 

C Programming Tricky Questions and Answers PDF for Interview

Frequently Asked Questions (FAQs)

Is 121 a palindrome number?

Yes, because reversing 121 gives 121.

Is 12321 a palindrome number?

Yes, it reads the same forward and backward.

Are negative numbers palindrome?

No, because the minus sign changes the reverse.

Final Notes for Interview Preparation

  • Always explain logic before code
    Mention time complexity
    Be ready with recursion version
    Practice dry run

Conclusion

The palindrome number in C is a fundamental interview problem that tests logic, loops, functions, and recursion.
By understanding all approaches-loops, functions, and recursion-you’ll be well-prepared for coding interviews, exams, and placements.

Pro Tip: If you understand palindrome number logic, you can easily solve:

  • Reverse number in C
  • Armstrong number in C
  • String palindrome in C

Leave a Comment

error: Content is protected !!