Skip to content

Check if number is palindrome in C#

Check if number is palindrome in C#  Learn how to  reverse a number and compare the number with the original number for deciding whether the number is same or not.

Check if number is palindrome in C#

A number is palindrome number, when its number is same when digits are reversed.

For example 565, if we reverse the digits of 565 then it is till 565. This kind of numbers we can call as palindrome numbers.
Few examples of palindrome numbers are 121,565,24542…

The approcah of finding whether the number is palindrome number or not is simple. for finding this just reverse the given number.

Check palindrome number algorithm

To test if a number is palindrome or not, do the following steps:
1. Get the number from a user.
2. Reverse it.
3. Compare it with the number entered by the user.
4. If both are the same then print palindrome number else print not a palindrome number.

Example program Check if number is palindrome in C#

Method 1 

using System;
 class Program
    {
        static void Main(string[] args)
        {
            int a = 565, reverseNumber=0, temp, rem;

            temp = a;
            while (temp != 0)
            {
                rem = temp % 10;
                temp = temp / 10;
                reverseNumber = reverseNumber * 10 + rem;
            
            }

            if(a==reverseNumber)
            {
                Console.WriteLine(a +
                " is a palindrome number");
            }
            else
            {
                Console.WriteLine(a + 
                "is not a palindrome number");
            }
        }
    }

Output
565 is a palindrome number

Method 2 c# program using function.

class Program
    {
        public int reverse_num = 0, rem;
        static void Main(string[] args)
        {
            int a = 565, reverseNumber=0, temp, rem;

            Program p = new Program();
            reverseNumber = p.check_palindrome(a);

            if(a==reverseNumber)
            {
               Console.WriteLine(a + 
               " is a palindrome number");
            }
            else
            {
               Console.WriteLine(a + 
              "is not a palindrome number");
            }
        }

    public int check_palindrome(int num)  
    {

        int t, reverse = 0;

        t = num;

        while (t != 0)
        {
            reverse = reverse * 10;
            reverse = reverse + t % 10;
            t = t / 10;
        }

        return reverse;
    }
    }

Method 3 -C# program using recursion

using System;
 class Program
    {
        public int reverse_num = 0, rem;
        static void Main(string[] args)
        {
            int a = 567, reverseNumber=0;

            Program p = new Program();
            reverseNumber = p.check_palindrome(a);

            if(a==reverseNumber)
            {
                Console.WriteLine(a +
                " is a palindrome number");
            }
            else
            {
                Console.WriteLine(a +
                "is not a palindrome number");
            }
        }

    public int check_palindrome(int num)  
    {         
  
            if(num!=0)
            {
                rem=num%10;
                reverse_num=reverse_num*10+rem;
                check_palindrome(num/10);
            }
            
            return reverse_num;
    }
    }

Published inC# Programming Examples