Check Palindrome Number in C#
What you’ll learn
- What a palindrome number is — same digits forward and backward (e.g.
121). - How to reverse digits with
% 10and/= 10insideIsPalindrome. - Two programs: check one value and list palindromes from 100 to 200, plus a live preview.
Overview
A palindrome number reads identically from left to right and right to left. The classic interview approach reverses the digits arithmetically and compares the result to the original — no strings required.
Reverse & compare
Build reversedNumber digit by digit; test original == reversed.
Live preview
Try 121, 123, or 9009 in the browser widget.
Range scan
Loop 100..200 and print every palindrome in the interval.
Prerequisites
Integer arithmetic, while loops, and basic for loops.
number % 10reads the last digit;number /= 10removes it.- Comfort with
boolhelpers andif/elsebranches.
What is a palindrome number?
Write the digits left to right, then right to left. If both strings match, the number is a palindrome.
121 → forward 1-2-1, backward 1-2-1 — palindrome. 123 → backward 3-2-1 — not a palindrome. In code we rebuild the backward form as an integer and compare.
Quick examples
Reversing 121 gives 121 again. Reversing 123 gives 321 — different, so not a palindrome. In the range 100..200, palindromes include 101, 111, 121, …, 191.
Live preview
Enter a nonnegative integer — the widget reverses digits the same way as the C# loop and compares.
Algorithm
Goal: decide whether integer n is a palindrome.
Save original
Store originalNumber = n before modifying n.
Reverse digits
While n != 0: append last digit to reversedNumber, then remove last digit from n.
Compare
Return true when originalNumber == reversedNumber.
📜 Pseudocode
original = n
reversed = 0
while n != 0:
digit = n % 10
reversed = reversed * 10 + digit
n = n / 10
return original == reversed Check a single number
IsPalindrome reverses digits and compares. Main tests 121 — matches the classic reference output.
using System;
class Program
{
static bool IsPalindrome(int number)
{
int originalNumber = number;
int reversedNumber = 0;
while (number != 0)
{
int remainder = number % 10;
reversedNumber = reversedNumber * 10 + remainder;
number /= 10;
}
return originalNumber == reversedNumber;
}
static void Main()
{
int number = 121;
if (IsPalindrome(number))
Console.WriteLine($"{number} is a palindrome number.");
else
Console.WriteLine($"{number} is not a palindrome number.");
}
} How the program works
- Save
originalNumberso you can still compare afternumberis stripped to zero. - Each loop iteration moves the last digit of
numberontoreversedNumber. - When
numberbecomes0, compare the two stored values.
Palindrome numbers from 100 to 200
Reuse IsPalindrome inside a for loop to print every palindrome in the range — the reference range walkthrough.
using System;
class Program
{
static bool IsPalindrome(int number)
{
int originalNumber = number;
int reversedNumber = 0;
while (number != 0)
{
int digit = number % 10;
reversedNumber = reversedNumber * 10 + digit;
number /= 10;
}
return originalNumber == reversedNumber;
}
static void Main()
{
Console.WriteLine("Palindrome Numbers in the range 100 to 200:");
for (int i = 100; i <= 200; i++)
{
if (IsPalindrome(i))
Console.Write($"{i} ");
}
Console.WriteLine();
}
} Optimization and variations
Half reversal. Advanced solutions reverse only half the digits and compare to the remaining half — fewer steps and less overflow risk on large values.
String approach. Converting to string and comparing characters works for learning but arithmetic reversal is the classic interview answer.
❓ FAQ
🔄 Input / output examples
| Input | Reversed | Result |
|---|---|---|
121 | 121 | palindrome |
123 | 321 | not palindrome |
0 | 0 | palindrome |
9009 | 9009 | palindrome |
Edge cases and pitfalls
n = 0
The loop never runs; reversedNumber stays 0 — palindrome.
0–9
All single-digit nonnegative integers are palindromes.
Large integers
reversedNumber * 10 + digit can overflow int for very large inputs — mention half-reversal in interviews.
⏱️ Time and space complexity
Reversing d digits takes O(d) time. Extra space is O(1) — only a few integer variables. Scanning a range adds a factor of the range size.
Summary
- A palindrome number reads the same forward and backward.
- Reverse digits with a loop, then compare to the original.
- Reuse
IsPalindromein a range loop to list all matches.
Single-digit numbers like 7 are always palindromes — they read the same forward and backward. So are values like 0 and 121.
8 people found this page helpful
