Introduction
In this article, we will learn how to check whether a number is a palindrome or not using c#. we will find its reverse number and then compare if the reverse of a number if equal to its value (actual number) or not if reverse and numbers are the same then the given number will be a palindrome.
For example:
- 121 & 121 (palindrome number)
- 456 & 654 (not palindrome number)
C# code Example :
First of creating a new console application and put the below code in the program.cs file.
using System; namespace PalindromeExamples { class Program { static void Main(string[] args) { int EnteredNumber, tempNumber, remainder, reverseNumber = 0; Console.WriteLine("\n"); Console.Write("Enter a any number : "); EnteredNumber = int.Parse(Console.ReadLine()); Console.WriteLine("\n"); tempNumber = EnteredNumber; //reverse num stored in reverseNumber variable while (EnteredNumber > 0) { remainder = EnteredNumber % 10; reverseNumber = reverseNumber * 10 + remainder; EnteredNumber /= 10; } Console.WriteLine("Reverse number is a : {0}", reverseNumber); Console.WriteLine("\n"); //Now check entered num and reverse num if (tempNumber == reverseNumber) { Console.WriteLine("This is a palindrome Number"); } else { Console.WriteLine("This is not a palindrome Number"); } Console.ReadLine(); } } }
Then, run your console application.