Kailash's Blogs

Check A Number OR Word For Palindrome In C#

Kailash Chandra Behera | Tuesday, June 11, 2024
Kailash's Blogs

Introduction

Here in this blog post check a number or word for palindrome, we will discuss the how to write a program in C# to check whether a given number is a palindrome number, or a given word is a palindrome word or not.

Getting Started

Before continuing to write code, lets know the palindromic meaning with palindrome example. A word or number is said to be palindrome if it remains same on reading from both end ( forward and backward). For example, a number will be called a palindromic number if digits of the number remain same when digits are reversed. Same as a word or string will be called a palindromic string or word if its letters remain same when the letters are reversed.

Examples of Palindromes

  1. 123454321
  2. Malayalam

Palindrome Number

The following code snippet checks a given number is a palindrome number or not.

 static void Main(string[] args)  
     {  
       Console.Write("Enter a Number : ");  
       int entered_number = int.Parse(Console.ReadLine());  
       int original_number = entered_number;  
       int remineder;  
       int reversed_number = 0;  
       while (entered_number > 0)  
       {  
         //Get the remainder by dividing the number with 10   
         remineder = entered_number % 10;  
         //Get the quotient by dividing the number with 10   
         entered_number = entered_number / 10;  
         //multiply the sum with 10 and then add the remainder  
         reversed_number = (reversed_number * 10) + remineder;  
       }  
       if (original_number == reversed_number)  
       {  
         Console.WriteLine($"Number {original_number} is Palindrome.");  
       }  
       else  
       {  
         Console.WriteLine($"Number {original_number} is not Palindrome");  
       }  
       Console.ReadKey();  
     }  

In the above code example is a console application where it stored the entered number in two different numeric variables. The fist variable (entered_number) is used to store the entered number and manipulate it to find palindrome. The second (original_number) variable is used to store the original input number and later will compare in a numberic variable what stores the number having reversed digits.

The while clous is used here to remove digits from the entered number and will continue looping until the variable value of the entered_number becomes zero.

The variable reminder is used to store the removable digit. The variable entered_number first divided by 10 to store the digit that will be removed into from entered_number variable.

Then to remove the number from the entered_number variable, the variable entered_number is quotient by 10.

Each removed digit is concatenated and stores in reversed_number variable. The reversed_number number is multiplied by 10 and added the result with reminder for concatenate purpose.

Finally, the original_number variable is compared with the reversed_number for palindrome.

Palindrome String

There are various ways to check a word or string for palindromic string. For example, you can check a word or string is palindrome word or not by using the existing reverse function of Array class or by using loop. Please check the below example

As I mentioned in above a string or word is said to be palindrome if it remains the same on reading from both ends, The following code example reverses the string values using the loop and checks for palindrome.

 static void Main(string[] args)  
 {  
      Console.Write("Enter a String : ");  
      string input_string = Console.ReadLine();  
      string reverse_string = string.Empty;  
      for (int i = input_string.Length - 1; i >= 0; i--)  
      {  
           reverse_string += input_string[i];  
      }  
      if (input_string == reverse_string)  
      {  
           Console.WriteLine($"{input_string} is Palindrome.");  
      }  
      else  
      {  
           Console.WriteLine($"{input_string} is not Palindrome");  
      }  
      Console.ReadKey();  
 }  

Useing Reverse Function

 static void Main()  
 {  
      Console.Write("Enter a string to Check Palindrome : ");  
      string inputString = Console.ReadLine();  
      char[] nameArray = inputString.ToCharArray();  
      Array.Reverse(nameArray);  
      string reverse = new string(nameArray);  
      if(inputString==reverse)  
      {  
           Console.WriteLine("'{0}' is Palindrome",inputString);  
      }  
      else  
      {  
           Console.WriteLine("'{0}' is not Palindrome", inputString);  
      }  
      Console.ReadKey();  
 }  

I hope the above two basic programs helped you write program to check a number or a word for Palindrom.

Thanks


No comments: