Explaining Palindrome Program in C#

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,

  1. Introduction
  2. What is Palindrome
    • Longest palindromes
  3. How to check if a given number is Palindrome or not in C#?
    • Algorithm to check Palindrome Number
    • Program for Palindrome Number in C#
  4. How to check if a given string is Palindrome or not in C#?
  5. 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.

  1. Get the input from the user that you want to check if it is a palindrome or not.
  2. Hold that number in a temporary variable
  3. Reverse that number with logic.
  4. Compare the temporary number with the reversed number.
  5. 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");
        }
    }
}

Palindrone in C#

Palindrone in C#

How to check if a given string is Palindrome or not in C#?

The following C# program will enable the user to enter a string and check whether that string is palindrome or not. Let's see.

Method 1) Using inbuilt functions

 internal class PalindromeStringInCsharp
 {
     static void Main(string[] args)
     {
         Console.Write("Enter a string to check for Palindrome: ");
         string str = Console.ReadLine();

         char[] newArray = str.ToCharArray();
         Array.Reverse(newArray);
         string reverseStr = new string(newArray);

         Console.WriteLine(str == reverseStr ? $"Yesss! String {str} is Palindrome" : $"Oopss! String {str} is not Palindrome");

         Console.ReadLine();
     }
 }

Palindrone in C#

Palindrone in C#

Method 2) Without using inbuilt function

internal class PalindromeStringInCsharpWithoutInBuiltFuntion
{
    static void Main(string[] args)
    {
        Console.Write("Enter a string to check for Palindrome: ");
        string str = Console.ReadLine();
        string reverseStr = string.Empty;

        foreach (char c in str)
        {
            reverseStr = c + reverseStr;
        }

        Console.WriteLine(str == reverseStr ? $"Yesss! String {str} is Palindrome" : $"Noooo! String {str} is not Palindrome");

        Console.ReadLine();
    }
}

Palindrone in C#

Palindrone in C#

See you in the next article, till then, take care and be happy learning.

You can connect with me @

Conclusion

In this article, we have discussed the Palindrome Program in C# (Palindrome Number and Palindrome String) withe various examples.

I hope you enjoyed this article. Follow C# Corner to learn more new and amazing things about C#.

Thanks for reading.


Similar Articles