Write a C# program to check if a number is Palindrome or not


First, find the reverse of the string to check if a string is a palindrome or not −

Array.reverse()

Now use the equals() method to match the original string with the reversed. If the result is true, that would mean the string is Palindrome.

Let us try the complete example. Here, our string is “Madam”, which is when reversed gives the same result −

Example

 Live Demo

using System;
namespace Demo {
   class Program {
      static void Main(string[] args) {
         string string1, rev;
         string1 = "Madam";
         char[] ch = string1.ToCharArray();

         Array.Reverse(ch);
         rev = new string(ch);

         bool b = string1.Equals(rev, StringComparison.OrdinalIgnoreCase);
         if (b == true) {
            Console.WriteLine("String " + string1 + " is a Palindrome!");
         } else {
            Console.WriteLine("String " + string1 + " is not a Palindrome!");
         }
         Console.Read();
      }
   }
}

Output

String Madam is a Palindrome!

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 20-Jun-2020

714 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements