Palindrome – What’s That?
A palindrome is a sequence of characters that reads the same forward and backwards. In the context of numbers, A palindrome number is the same number when reversed.
- Palindrome number examples: 121, 131, 34543, etc.
- Palindrome string examples include “level,” “radar,” and “madam.”
In this article, we’ll learn how to write a Palindrome program in C#.
Table of Contents
Method1: Writing Palindrome program in C#:
Now, let’s dive into the C# code to reverse a number and check if it’s a palindrome.
using System;
class Program
{
static void Main()
{
// Ask the user to enter a number
Console.WriteLine("Enter a number to check for palindrome: ");
int numToCheck = int.Parse(Console.ReadLine());
// Store the original number for later comparison
int originalNum = numToCheck;
int reversedNum = 0;
// Reverse the number
while (numToCheck > 0)
{
int remainder = numToCheck % 10;
reversedNum = (reversedNum * 10) + remainder;
numToCheck = numToCheck / 10;
}
// Check if the original and reversed numbers are the same
if (originalNum == reversedNum)
{
Console.WriteLine("The number is a palindrome.");
}
else
{
Console.WriteLine("The number is not a palindrome.");
}
}
}
Output:
Enter a number to check for palindrome:
121
The number is a palindrome.
Code Explanation:
We have written the above C# program to check if a given number is a palindrome:
- The program first asks the user to input a number.
- The original number is stored for later comparison.
- A while loop is used to reverse the digits of the input number.
- The reversed number is compared with the original number to determine if it’s a palindrome.
- After the comparison, the result is printed to the console.
Method2: Check palindrome number using string conversion
There are multiple ways to write a C# program to check if a given number is a palindrome. Here is an alternative approach using a string conversion method:
using System;
class Program
{
static void Main()
{
// Prompt the user to enter a number
Console.WriteLine("Enter a number to check for palindrome: ");
int numToCheck = int.Parse(Console.ReadLine());
// Convert the number to a string for easy comparison
string originalString = numToCheck.ToString();
string reversedString = ReverseString(originalString);
// Check if the original and reversed strings are the same
if (originalString.Equals(reversedString))
{
Console.WriteLine("The number is a palindrome.");
}
else
{
Console.WriteLine("The number is not a palindrome.");
}
}
// Function to reverse a string
static string ReverseString(string str)
{
char[] charArray = str.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}
}
Output:
Enter a number to check for palindrome:
34543
The number is a palindrome.
Code Explanation:
- The above program converts the original number to a string using
ToString()
. - The
ReverseString()
function is used to reverse the string. - The original and reversed strings are compared using the Equals method to check if it is a palindrome.
- The result is then printed to the console.
Recommended Articles:
- Fibonacci sequence: Fibonacci series in C# (with examples)
- How to remove duplicate characters from a String in C#
- Palindrome program in C# with examples
- C# Program to Check if a Given Number is Even or Odd
- C# program to count the occurrences of each character in a String
- C# program to count vowels and consonants in a string
- How to remove special characters from a string in C#
- Program to copy all elements of an array into another array
- Different Ways to Calculate Factorial in C# (with Full Code Examples)
- C# Programs asked in Interviews
Latest posts by Shekh Ali (see all)
- Difference Between Array And ArrayList In C#: Choosing the Right Collection - May 28, 2024
- C# Program to Capitalize the First Character of Each Word in a String - February 21, 2024
- C# Program to Find the Longest Word in a String - February 19, 2024