In this blog, we will learn How to check a number is Palindrome or not in C#. This Is also one of the most popular interview questions among the interviewers. The basic approach is to reverse the number and then compare it with the number added by the user.
int Number, TmpNumber, Reverse; Console.WriteLine("Enter
a No. to check either it's palindrome or not"); //Get number from the user Number = int.Parse(Console.ReadLine()); //Hold the number in the TmpNumber TmpNumber = Number; Reverse = 0; //Reverse the Number while (Number > 0) { int remainder = Number % 10; Reverse = (Reverse *
10) + remainder; Number = Number / 10; } //Compare the Reversed number with the number entered by the
user if (TmpNumber == Reverse) { Console.WriteLine("'{0}' no. is an example of Palindrome", TmpNumber); } else { Console.WriteLine("'{0}' no. is not an example of Palindrome", TmpNumber); } //Console.ReadLine() to hold the screen after the execution of
the program Console.ReadLine(); |
0 comments:
Post a Comment