Palindrome program in C# with examples

Palindrome – What’s That?

A palindrome is a sequence of characters that reads the same forward and backwards. In the context of numbers, A palindrome number is the same number when reversed.

  • Palindrome number examples: 121, 131, 34543, etc.
  •  Palindrome string examples include “level,” “radar,” and “madam.”

In this article, we’ll learn how to write a Palindrome program in C#.

Method1: Writing Palindrome program in C#:

Now, let’s dive into the C# code to reverse a number and check if it’s a palindrome.

using System;

class Program
{
    static void Main()
    {
        // Ask the user to enter a number
        Console.WriteLine("Enter a number to check for palindrome: ");
        int numToCheck = int.Parse(Console.ReadLine());

        // Store the original number for later comparison
        int originalNum = numToCheck;
        int reversedNum = 0;

        // Reverse the number
        while (numToCheck > 0)
        {
            int remainder = numToCheck % 10;
            reversedNum = (reversedNum * 10) + remainder;
            numToCheck = numToCheck / 10;
        }

        // Check if the original and reversed numbers are the same
        if (originalNum == reversedNum)
        {
            Console.WriteLine("The number is a palindrome.");
        }
        else
        {
            Console.WriteLine("The number is not a palindrome.");
        }
    }
}

Output:

Enter a number to check for palindrome: 
121
The number is a palindrome.

Code Explanation:

We have written the above C# program to check if a given number is a palindrome:

  • The program first asks the user to input a number.
  • The original number is stored for later comparison.
  • A while loop is used to reverse the digits of the input number.
  • The reversed number is compared with the original number to determine if it’s a palindrome.
  • After the comparison, the result is printed to the console.

Method2: Check palindrome number using string conversion

There are multiple ways to write a C# program to check if a given number is a palindrome. Here is an alternative approach using a string conversion method:

using System;

class Program
{
    static void Main()
    {
        // Prompt the user to enter a number
        Console.WriteLine("Enter a number to check for palindrome: ");
        int numToCheck = int.Parse(Console.ReadLine());

        // Convert the number to a string for easy comparison
        string originalString = numToCheck.ToString();
        string reversedString = ReverseString(originalString);

        // Check if the original and reversed strings are the same
        if (originalString.Equals(reversedString))
        {
            Console.WriteLine("The number is a palindrome.");
        }
        else
        {
            Console.WriteLine("The number is not a palindrome.");
        }
    }

    // Function to reverse a string
    static string ReverseString(string str)
    {
        char[] charArray = str.ToCharArray();
        Array.Reverse(charArray);
        return new string(charArray);
    }
}

Output:

Enter a number to check for palindrome: 
34543
The number is a palindrome.

Code Explanation:

  • The above program converts the original number to a string using ToString().
  • The ReverseString() function is used to reverse the string.
  • The original and reversed strings are compared using the Equals method to check if it is a palindrome.
  • The result is then printed to the console.

Recommended Articles:

Shekh Ali
5 1 vote
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments