In this article, I am going to explain the Palindrome Program in C# (Palindrome Number and Palindrome String) with Examples. This detailed article will cover the following topics,
- Introduction
- What is Palindrome
- Longest palindromes
- How to check if a given number is Palindrome or not in C#?
- Algorithm to check Palindrome Number
- Program for Palindrome Number in C#
- How to check if a given string is Palindrome or not in C#?
- Conclusion
What is Palindrome?
As per Wikipedia, "A palindrome is a word, number, phrase, or other sequence of symbols that reads the same backwards as forwards, such as madam or racecar, the date "22/02/2022" The word palindrome was introduced by English poet and writer Henry Peacham in 1638.".
- Palindrome Number: A palindrome number is a number which remains the same even after reversing the digits of that number. For Example, 121, 232, 12321, etc.
- Palindrome String: A palindrome string is a string that remains the same even after reversing that string. For Example, ABA, MADAM, RACECAR, etc.
Longest Palindromes
According to the Guinness Book of World Records, the longest palindromic word in common use worldwide is the 19-letter Finnish word "saippuakivikaupias," which refers to a soapstone seller.
The longest single-word palindrome in the Oxford English Dictionary is tattarrattat, which is 12 letters long and comes from James Joyce's Ulysses.
How to check if a given number is Palindrome or not in C#?
Algorithm to check Palindrome Number
To check whether the given number is palindrome or not, the steps are given below.
- Get the input from the user that you want to check if it is a palindrome or not.
- Hold that number in a temporary variable
- Reverse that number with logic.
- Compare the temporary number with the reversed number.
- If both numbers are equal then print it as palindrome number, else print it as non palindrome number.
Program for Palindrome Number in C#
The following C# program will enable the user to enter a number and check whether that number is a palindrome number or not. Let's see.
internal class PalindromeNumberInCsharp
{
static void Main(string[] args)
{
try
{
Console.Write("Enter a number to check for palindrome: ");
int number = Convert.ToInt32(Console.ReadLine());
int reminder, sum = 0;
int tempNumber = number;
while (number > 0)
{
reminder = number % 10;
sum = (sum * 10) + reminder;
number = number / 10;
}
if (tempNumber == sum)
{
Console.WriteLine($"Yesss! Number {tempNumber} is Palindrome");
}
else
{
Console.WriteLine($"Oopss! Number {tempNumber} is not Palindrome");
}
Console.ReadLine();
}
catch (Exception e)
{
Console.WriteLine("Oopss! Error Occured in Program");
}
}
}






Join the conversation! Your thoughts help the community grow.