Palindrome Number in C#

Introduction

In this article, we are going to explore how to determine whether a number is a Palindrome or not.

This is an essential technical interview question that may be posed to beginner, intermediate, and experienced candidates.

We previously covered how to reverse a string in the last article,

  1. How to Reverse Number in C#
  2. How to Reverse a String in C# 
  3. Palindrome String Program in C#

We will discuss

  1. What is a Palindrome Number?
  2. How do you check whether a Number is Palindrome or not?

Let’s understand.

What is a Palindrome Number?

In simple words, the Number will remain the same when reading from both sides. Let's see the below example of a Palindrome Number,

Example of the Palindrome Numbers

  1. 12321
  2. 78587

I hope you comprehended what constitutes a palindrome number. Now, we will create a C# example to accomplish this,

How to check whether a number is Palindrome or not?

First, create the ASP.NET Core Console application and write the below code in the program.cs file,

Console.WriteLine("Enter a Number");
var number = Convert.ToInt32(Console.ReadLine()); 
var tempNumbr= number;

var reverseNumber = 0;

while (tempNumbr != 0)
{
    var rem = tempNumbr % 10;
    reverseNumber = reverseNumber*10 + rem;
    tempNumbr= tempNumbr / 10;
}

if (number==reverseNumber)
{
    Console.WriteLine("Palindrome number");
}
else
{
    Console.WriteLine("NOT Palindrome number");
}
Console.ReadLine();

Output

Palindrome Number in C#

Execute the program using different numbers and see the output.

Palindrome Number in C#

That’s all for this article. I hope you enjoy this article and learn something new.


Similar Articles