Implement a C# function to check whether a given string is a palindrome. A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward.
home / developersection / forums / palindrome checker
Implement a C# function to check whether a given string is a palindrome. A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward.
Ashutosh Kumar Verma
14-Jun-2024Palindrome 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 if the inverted number is the same as the original number. Here is a quick step-by-step process.
Example-
Here is a simple c# example to demonstrate whether a given number is palindrome or not,
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-
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