0
0
CsharpProgramBeginner · 2 min read

C# Program to Check Palindrome Number

You can check if a number is a palindrome in C# by reversing the number using a loop and comparing it to the original number, for example: int reversed = 0; int temp = num; while (temp > 0) { reversed = reversed * 10 + temp % 10; temp /= 10; } bool isPalindrome = (num == reversed);.
📋

Examples

Input121
Output121 is a palindrome number.
Input123
Output123 is not a palindrome number.
Input0
Output0 is a palindrome number.
🧠

How to Think About It

To check if a number is a palindrome, think of reading the number from left to right and right to left. If both are the same, the number is a palindrome. We reverse the number by taking its last digit repeatedly and building a new number, then compare it to the original.
📐

Algorithm

1
Get the input number.
2
Store the original number in a temporary variable.
3
Initialize a variable to hold the reversed number as 0.
4
While the temporary variable is greater than 0:
5
Extract the last digit and add it to the reversed number after shifting digits.
6
Remove the last digit from the temporary variable.
7
Compare the reversed number with the original number.
8
If they are equal, the number is a palindrome; otherwise, it is not.
💻

Code

csharp
using System;
class Program {
    static void Main() {
        int num = 121;
        int reversed = 0, temp = num;
        while (temp > 0) {
            reversed = reversed * 10 + temp % 10;
            temp /= 10;
        }
        if (num == reversed) Console.WriteLine($"{num} is a palindrome number.");
        else Console.WriteLine($"{num} is not a palindrome number.");
    }
}
Output
121 is a palindrome number.
🔍

Dry Run

Let's trace the number 121 through the code to see how it checks for palindrome.

1

Initialize variables

num = 121, reversed = 0, temp = 121

2

First loop iteration

reversed = 0 * 10 + 121 % 10 = 1, temp = 121 / 10 = 12

3

Second loop iteration

reversed = 1 * 10 + 12 % 10 = 12, temp = 12 / 10 = 1

4

Third loop iteration

reversed = 12 * 10 + 1 % 10 = 121, temp = 1 / 10 = 0

5

Compare original and reversed

num = 121, reversed = 121, they are equal, so palindrome

tempreversed
1210
121
112
0121
💡

Why This Works

Step 1: Reverse the number

We build the reversed number by taking the last digit of the original number and adding it to the reversed number shifted by one digit using reversed = reversed * 10 + temp % 10.

Step 2: Remove last digit

We reduce the original number by removing its last digit using temp /= 10 to process the next digit.

Step 3: Compare original and reversed

If the reversed number equals the original number, it means the number reads the same forwards and backwards, so it is a palindrome.

🔄

Alternative Approaches

String reversal
csharp
using System;
class Program {
    static void Main() {
        int num = 121;
        string str = num.ToString();
        char[] arr = str.ToCharArray();
        Array.Reverse(arr);
        string revStr = new string(arr);
        if (str == revStr) Console.WriteLine($"{num} is a palindrome number.");
        else Console.WriteLine($"{num} is not a palindrome number.");
    }
}
This method converts the number to a string and reverses it, which is simpler but uses extra memory for the string.
Recursive check
csharp
using System;
class Program {
    static bool IsPalindrome(int num, int rev = 0) {
        if (num == 0) return true;
        rev = rev * 10 + num % 10;
        return IsPalindrome(num / 10, rev) && (num == 0 || num == rev);
    }
    static void Main() {
        int num = 121;
        Console.WriteLine(IsPalindrome(num) ? $"{num} is a palindrome number." : $"{num} is not a palindrome number.");
    }
}
This recursive method is more complex and less efficient but shows a different approach.

Complexity: O(d) time, O(1) space

Time Complexity

The time depends on the number of digits d in the number because the loop runs once per digit to reverse it.

Space Complexity

Only a few integer variables are used, so space is constant O(1).

Which Approach is Fastest?

The integer reversal method is faster and uses less memory than string conversion, while recursion is less efficient and more complex.

ApproachTimeSpaceBest For
Integer reversalO(d)O(1)Performance and memory efficiency
String reversalO(d)O(d)Simplicity and readability
Recursive checkO(d)O(d)Learning recursion, less practical
💡
Use integer operations to reverse the number instead of converting to string for better performance.
⚠️
Forgetting to reset the temporary variable or comparing the reversed number with the wrong value causes incorrect results.