C# - Palindrome Number Program

Given a number, we have to check whether it is a Palindrome or not using C# program?
[Last updated : March 19, 2023]

To understand the program of palindrome number, first we should understand the concept of palindrome number.

What are Palindrome Number?

Palindrome numbers are those numbers that are equal to its reverse.

For example:

  • 121 is palindrome number.
  • 112 is not a palindrome number.
  • 12321 is a palindrome number.
  • 12345 is not a palindrome number.

In this program, we will read an integer number and check whether it is Palindrome or not. To check palindrome, we will find its reverse number and then compare if reverse of a number if equal to its value (actual number) or not, if reverse and numbers are same then given number will be palindrome.

C# program to check whether a given number is Palindrome or not

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    
    class Program
    {
        static void Main(string[] args)
        {
            int     number    = 0;
            int     tNumber   = 0;
            int     rem       = 0;
            int     rev       = 0;
            

            Console.Write("Enter Number : ");
            tNumber = number = int.Parse(Console.ReadLine());

            //To find out total number of digits in number
            while (number > 0)
            {
                rem = number %10;
                rev = rev * 10 + rem;
                number = number / 10;
                
            }

            
            if (rev == tNumber)
                Console.WriteLine("Given Number is Palindrome");
            else
                Console.WriteLine("Given Number is not a Palindrome");
        }
    }
}

Output

Enter Number : 12321
Given Number is Palindrome

C# Basic Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.