How to check if a number is palindrome in C#

2 Answers

0 votes
using System;

public class Program
{
	public static bool isPalindrome(int num) {
		int reverse = 0, savednum = num;
 
        while (num > 0) { 
            reverse = reverse * 10 + (num % 10);
            num /= 10;
        }
 
        return savednum == reverse;
	}

	public static void Main(string[] args)
	{
		int n = 12321;

		Console.WriteLine((isPalindrome(n) == true ? "Yes" : "No"));
	}
}





/*
run:
    
Yes
    
*/


answered Apr 9, 2014 by avibootz
edited Jan 8, 2024 by avibootz
0 votes
using System;
using System.Linq;

public class Program
{
	public static bool isPalindrome(int n) {
		string strn = n.ToString();
		string reversestr = String.Join("", strn.Reverse());

		return strn == reversestr;
	}

	public static void Main(string[] args)
	{
		int n = 12321;

		Console.WriteLine((isPalindrome(n) == true ? "Yes" : "No"));
	}
}





/*
run:
    
Yes
    
*/

 



answered Jan 8, 2024 by avibootz
...