Check Palindrome Number in C#

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 2 Code Examples
Digit reversal

What you’ll learn

  • What a palindrome number is — same digits forward and backward (e.g. 121).
  • How to reverse digits with % 10 and /= 10 inside IsPalindrome.
  • 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 % 10 reads the last digit; number /= 10 removes it.
  • Comfort with bool helpers and if/else branches.

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.

Palindrome 121, 7, 9009
Not palindrome 123, 1001
Test original == reversed

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.

Try 123 (not palindrome) or 0 (palindrome).

Live result
Press “Check palindrome”.

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

Pseudocode
original = n
reversed = 0
while n != 0:
    digit = n % 10
    reversed = reversed * 10 + digit
    n = n / 10
return original == reversed
1

Check a single number

IsPalindrome reverses digits and compares. Main tests 121 — matches the classic reference output.

c#
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

  1. Save originalNumber so you can still compare after number is stripped to zero.
  2. Each loop iteration moves the last digit of number onto reversedNumber.
  3. When number becomes 0, compare the two stored values.
2

Palindrome numbers from 100 to 200

Reuse IsPalindrome inside a for loop to print every palindrome in the range — the reference range walkthrough.

c#
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

A nonnegative integer whose decimal digits read the same forward and backward — for example 121, 9009, or 7.
Rebuilding the number from digits right-to-left gives the backward form. If it equals the original, the number is a palindrome.
Yes. Zero reads the same both ways, and the reverse-digit loop leaves reversed equal to 0.
Yes. Any one-digit nonnegative integer (0 through 9) is a palindrome.
This tutorial focuses on nonnegative integers. Negative values are usually excluded because of the minus sign.
For very large ints, reversed * 10 + digit can overflow. Interview answers often assume normal-sized inputs; production code may compare half the digits instead.
O(d) where d is the number of digits — you peel one digit per loop iteration.

🔄 Input / output examples

InputReversedResult
121121palindrome
123321not palindrome
00palindrome
90099009palindrome

Edge cases and pitfalls

Zero

n = 0

The loop never runs; reversedNumber stays 0 — palindrome.

Single digit

0–9

All single-digit nonnegative integers are palindromes.

Overflow

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 IsPalindrome in a range loop to list all matches.
Did you know?

Single-digit numbers like 7 are always palindromes — they read the same forward and backward. So are values like 0 and 121.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful