Do you want to know how to check the palindrome number in C#? In this tutorial, I will explain how to write a C# program to check palindrome numbers.
To check if a number is a palindrome in C#, you can create a method that reverses the number and compares it with the original. The program involves reading the input number, reversing it by modulo and division operations, and then checking if the reversed number is equal to the original. If they match, the number is a palindrome.
What is a Palindrome Number in C#?
A palindrome number is a number that remains the same when its digits are reversed. Examples include 121, 12321, or 4554.
C# Program To Check Palindrome Number
Now, let us see how to write a program to check palindrome number in C#. Here are the 4 steps to check whether a number is a palindrome number or not in C#.
Step 1: Accepting the Number
We need to accept a number from the user. This can be done using the Console.ReadLine()
method.
static int ReadNumber()
{
Console.Write("Enter a number: ");
return int.Parse(Console.ReadLine());
}
Step 2: Reversing the Number
The next step is to reverse the number. This is a key step in checking for a palindrome.
static int ReverseNumber(int number)
{
int reverse = 0;
while (number > 0)
{
int remainder = number % 10;
reverse = (reverse * 10) + remainder;
number /= 10;
}
return reverse;
}
Step 3: Checking for Palindrome
Now, we compare the original number with the reversed number. If they are the same, the number is a palindrome.
static bool IsPalindrome(int number)
{
return number == ReverseNumber(number);
}
Step 4: The Main Method
Finally, we need to tie all these methods together in the Main
method.
static void Main(string[] args)
{
int number = ReadNumber();
bool isPalindrome = IsPalindrome(number);
if (isPalindrome)
Console.WriteLine($"{number} is a Palindrome.");
else
Console.WriteLine($"{number} is not a Palindrome.");
}
Here’s a complete C# program that checks if a given number is a palindrome.
using System;
class PalindromeChecker
{
static void Main(string[] args)
{
int number = ReadNumber();
bool isPalindrome = IsPalindrome(number);
if (isPalindrome)
Console.WriteLine($"{number} is a Palindrome.");
else
Console.WriteLine($"{number} is not a Palindrome.");
Console.ReadLine(); // Keep the console open
}
static int ReadNumber()
{
Console.Write("Enter a number: ");
return int.Parse(Console.ReadLine());
}
static int ReverseNumber(int number)
{
int reverse = 0;
while (number > 0)
{
int remainder = number % 10;
reverse = (reverse * 10) + remainder;
number /= 10;
}
return reverse;
}
static bool IsPalindrome(int number)
{
return number == ReverseNumber(number);
}
}
When you run the program using the C# console application using Visual Studio, it will prompt you to enter a number and then output whether the number is a palindrome or not.
Here it prompts me to enter a number, and it shows me that it is a palindrome number like the one below screenshot.
Conclusion
This C# program is a straightforward example of how to check for palindrome numbers in C#. It demonstrates basic C# concepts like methods, loops, and conditional statements.
You may also like:
- Write A C# Program To Find The Largest Of Three Numbers
- Write A Program To Find Second Largest Number In An Array In C#
- How to Write a Program to Find a Leap Year in C#
Bijay Kumar is a renowned software engineer, accomplished author, and distinguished Microsoft Most Valuable Professional (MVP) specializing in SharePoint. With a rich professional background spanning over 15 years, Bijay has established himself as an authority in the field of information technology. He possesses unparalleled expertise in multiple programming languages and technologies such as ASP.NET, ASP.NET MVC, C#.NET, and SharePoint, which has enabled him to develop innovative and cutting-edge solutions for clients across the globe. Read more…