---
title: "Palindrome Checker"  
description: "Palindrome Checker"  
author: "Steilla Mitchel"  
published: 2024-06-11  
updated: 2024-06-14  
canonical: https://www.mindstick.com/forum/160724/palindrome-checker  
category: "c#"  
tags: ["c#", ".net"]  
reading_time: 3 minutes  

---

# Palindrome Checker

Implement a C# [function to check](https://www.mindstick.com/forum/161900/write-a-function-to-check-if-a-number-is-prime-with-explanation) whether a given [string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) is a palindrome. A palindrome is a [word](https://www.mindstick.com/forum/305/read-word-file), phrase, number, or other [sequence](https://www.mindstick.com/interview/34504/difference-between-identity-vs-sequence) of [characters](https://answers.mindstick.com/qa/42112/who-is-the-originator-of-avengers-characters) that reads the same [forward](https://www.mindstick.com/forum/33488/in-webview-disabling-uitoolbar-s-back-and-forward-butttons) and backward.

## Replies

### Reply by Ashutosh Patel

#### Palindrome Checker in C#

A **palindrome** number is a number that remains the same when its characters are reversed. In other words, it reads the same thing forward and backward.

#### Steps to Check Palindrome Number

To determine if a number is a **palindrome**, you can invert its digits and [check](https://yourviews.mindstick.com/story/2248/never-forget-to-check-these-specifications-before-buying-a-mobile-phone) if the inverted number is the same as the original number. Here is a quick step-by-step process.

1. Take the first number.
2. Turn his digits back.
3. Compare the opposite number to the first number.
4. If they are equal, the number is a palindrome.

## Example-

Here is a simple c# example to demonstrate whether a given number is palindrome or not,

```cs
using System;
class Program
{
   static void Main()
   {
       // take input from user and check where valid integer value or not
       Console.WriteLine("Enter an integer to check if it is a palindrome:");
       bool isSuccess = int.TryParse(Console.ReadLine(), out int number);
       if (isSuccess)
       {
           // if enter valid value the check number is palindrome
           if (IsPalindrome(number))
           {
               Console.WriteLine("{0} is a palindrome.", number);
           }
           else
           {
               Console.WriteLine("{0} is not a palindrome.", number);
           }
       }
       else
       {
           Console.WriteLine("Invalid input. Please enter a valid integer.");
       }
   }
   // function that take input number as argument
   static bool IsPalindrome(int number)
   {
       // Negative numbers are not considered palindromes
       if (number < 0)
       {
           return false;
       }
       // Get the reversed number
       int reversed = ReverseNumber(number);
       // Compare the original number with the reversed number
       return number == reversed;
   }
   // function that reverse the entered number
   static int ReverseNumber(int number)
   {
       int reversed = 0;
       while (number > 0)
       {
           int remainder = number % 10;
           reversed = (reversed * 10) + remainder;
           number /= 10;
       }
       return reversed;
   }
}
```

## In the above example-

**Main Method-** Asks the user to input an integer and verifies that the input is a valid integer using int.TryParse. If true, the IsPalindrome method checks whether the integer is a palindrome.

**IsPalindromeMethod-** Checks if the integer is negative (since negative numbers are not treated as palindromes) and then compares the integer to its opposite value

**ReverseNumber Method-** Take the digits of an integer multiple times (using modulus 10), add them to the opposite number (multiply 10 each iteration to shift the digits left), and then subtract the last digit of the original number (use integer line 10).

## Output-

```plaintext
1. Enter an integer to check if it is a palindrome:
	123abc
	Invalid input. Please enter a valid integer.
	----------------------------------------------------------------
2. Enter an integer to check if it is a palindrome:
	1234
	1234 is not a palindrome.
	----------------------------------------------------------------
3. Enter an integer to check if it is a palindrome:
	12321
	12321 is a palindrome.
```

This method works directly with an integer value without converting it to a string, making it possible to check if the integer is a palindrome.

**Also, Read:** [Roman to Integer](https://www.mindstick.com/forum/160726/roman-to-integer)


---

Original Source: https://www.mindstick.com/forum/160724/palindrome-checker

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
