What is palindrome :A palindrome is a word, phrase, number or sequence of words that reads the same backwards as forwards. Punctuation and spaces between the words or lettering is allowed.
Example 1: First example checks whether the number entered by the user is palindrome.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | int number, rem, sum = 0, gcc; Console.WriteLine("\n ### Check Palindrom Number ###"); Console.Write("\n Enter a Number: "); number = Convert.ToInt32(Console.ReadLine()); gcc = number; while (number > 0) { rem = number % 10; number = number / 10; sum = sum * 10 + rem; } if (gcc == sum) { Console.WriteLine("\n Number is Palindrom \n\n"); } else { Console.WriteLine("\n Number is not Palindrom\n\n"); } Console.ReadLine(); |
Example 2: Checking for Palindrome Strings or Numbers in C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | static void Main(string[] args) { Console.Write("Enter something for to check that is it palindrome :"); string text = Console.ReadLine(); int len = text.Length; bool flag = true; //check palindrome for (int i = 0; i < len/2; i++) { if (text[i] != text[len - (i + 1)]) { flag = false; break; } } //if flag true, text is palindrome if (flag) { Console.WriteLine("{0} is palindrome", text); } else { Console.WriteLine("{0} is not palindrome", text); } Console.ReadLine(); } |
Example 2: Display Palindrome Number Between Two Intervals (Palindrome Number Series in C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | static void Main(string[] args) { // Display Palindrom Number Between Two Intervals int num1,num2, n, rev_no, r; Console.WriteLine("Enter the start number: "); num1=Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter the finish number: "); num2 = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Palindrome Number from {0} to {1}",num1,num2); for (int i = num1; i <= num2; i++) { rev_no = 0; n = i; while (n != 0) { r = n % 10; rev_no = rev_no * 10 + r; n = n / 10; } if (i == rev_no) Console.Write(i + " "); } Console.ReadKey(); } |