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,
- How to Reverse Number in C#
- How to Reverse a String in C#
- Palindrome String Program in C#
We will discuss
- What is a Palindrome Number?
- 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
- 12321
- 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
Execute the program using different numbers and see the output.
That’s all for this article. I hope you enjoy this article and learn something new.