Home Programs How to check if a number is a Palindrome in C#

How to check if a number is a Palindrome in C#

In this article we will show you how show to determine if a number is a palindrome

A palindromic number is a number (such as 121) that remains the same when its digits are reversed.

The term palindromic is derived from palindrome, which refers to a word (such as rotor) whose spelling is unchanged when its letters are reversed.

The first 30 palindromic numbers (in decimal) are:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121, 131, 141, 151, 161, 171, 181, 191, 202

Example

 

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            int myNumber, r, sumNumber = 0, tempNumber;
            // get the user input
            Console.Write("Enter a Number: ");
            myNumber = int.Parse(Console.ReadLine());
            tempNumber = myNumber;
            while (myNumber > 0)
            {
                r = myNumber % 10;
                sumNumber = (sumNumber * 10) + r;
                myNumber = myNumber / 10;
            }
            // display the  result to the user
            if (tempNumber == sumNumber)
                Console.Write("The Number is a Palindrome.");
            else
                Console.Write("The Number is not a Palindrome");

            Console.ReadLine();
        }
    }
}

 

Here is a test run

Enter a Number: 121
The Number is a Palindrome.

Enter a Number: 122
The Number is not a Palindrome

You may also like