Palindrome in C#
Palindrome in C#:
A palindrome is a sequence of characters (which can be a word, number, or phrase) that reads the same forwards and backwards, ignoring spaces, punctuation, and capitalization. A palindrome would be a number that remains the same when its digits are reversed.
Converting the number to a string.
string Orginal = number.ToString();
Converting the string into a character array.
char[] arr = Orginal.ToCharArray();
Reversing the character array.
Array.Reverse(arr);
Comparing the original string with the reversed string.
bool isPalindrome = (Orginal == new string(arr));
Coding:
using System;
public class Program
{
public static void Main()
{
int number = 101;
string Orginal = number.ToString();
char[] arr = Orginal.ToCharArray();
Array.Reverse(arr);
bool isPalindrome = (Orginal == new string(arr));
string results = isPalindrome ? "Palindrome" : "Not Palindrome";
Console.WriteLine(Orginal + " is " + results);
}
}
Comments
Post a Comment