Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Write a C# program to check if a number is Palindrome or not
A palindrome is a number or string that reads the same forward and backward. In C#, we can check if a number is a palindrome by reversing its digits and comparing it with the original number.
Syntax
Following is the syntax for reversing a string using Array.Reverse() method −
char[] charArray = string.ToCharArray(); Array.Reverse(charArray); string reversedString = new string(charArray);
Following is the syntax for comparing strings ignoring case −
bool isEqual = string1.Equals(string2, StringComparison.OrdinalIgnoreCase);
Using String Reversal Method
The most straightforward approach is to convert the number to a string, reverse it, and compare with the original −
using System;
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!");
}
}
}
The output of the above code is −
String Madam is a Palindrome!
Using Numeric Approach for Numbers
For numeric palindromes, we can reverse the digits mathematically without converting to string −
using System;
class Program {
static void Main(string[] args) {
int number = 12321;
int originalNumber = number;
int reversedNumber = 0;
while (number > 0) {
int digit = number % 10;
reversedNumber = reversedNumber * 10 + digit;
number = number / 10;
}
if (originalNumber == reversedNumber) {
Console.WriteLine(originalNumber + " is a Palindrome!");
} else {
Console.WriteLine(originalNumber + " is not a Palindrome!");
}
}
}
The output of the above code is −
12321 is a Palindrome!
Using Two-Pointer Approach
For strings, we can use two pointers moving from both ends toward the center to check characters −
using System;
class Program {
static void Main(string[] args) {
string text = "racecar";
bool isPalindrome = true;
int left = 0;
int right = text.Length - 1;
while (left < right) {
if (char.ToLower(text[left]) != char.ToLower(text[right])) {
isPalindrome = false;
break;
}
left++;
right--;
}
if (isPalindrome) {
Console.WriteLine(text + " is a Palindrome!");
} else {
Console.WriteLine(text + " is not a Palindrome!");
}
}
}
The output of the above code is −
racecar is a Palindrome!
Comparison of Approaches
| Approach | Best For | Time Complexity | Space Complexity |
|---|---|---|---|
| String Reversal | Simple cases, readability | O(n) | O(n) |
| Numeric Method | Integer palindromes | O(log n) | O(1) |
| Two-Pointer | Memory efficiency | O(n) | O(1) |
Conclusion
Checking for palindromes in C# can be done using string reversal, numeric digit manipulation, or two-pointer comparison. The choice depends on whether you're working with strings or numbers, and whether memory efficiency is important for your application.
