Author : Rahul

Write a program to check whether entered number is palindrome or not


Palindrome Number

A number is said to be Palindrome if the reverse of the number is the same as the original number. For Example, 2002 is the Palindrome Number because the reverse of 2002 is 2002 which is the same as the original number 2002.

Program

using System;

namespace ProgrammingQuestion
{
 class Program
 {
   static void Main(string[] args)
   {
     //Variable Declaration
     string checkVal, message;
     int checkNum, digit, reverseNum = 0, tempNum = 0, power;
     bool isPalindrome;
     
     //User Input
     Console.Write("Enter Number:");
     checkVal = Console.ReadLine();
     checkNum = Convert.ToInt32(checkVal);
     
     //Palindrome Logic
     tempNum = checkNum;
     while(tempNum > 0)
     {
       digit = tempNum % 10;
       power = GetPower(tempNum);
       reverseNum = reverseNum + (int)Math.Pow(10, power - 1) * digit;
       tempNum = tempNum / 10;
     }
     isPalindrome = checkNum == reverseNum;
     
     //Print Result
     if(isPalindrome)
     {
       message = $"Entered Number {checkNum} is a palindrome number.";
     }
     else
     {
       message = $"Entered Number {checkNum} is not a palindrome number.";
     }
     Console.WriteLine(message);
     Console.Read();
   }
   
   //Method is used to get the number of digit of the number
   private static int GetPower(int number)
   {
     int pow = 0;
     while(number > 0)
     {
       pow++;
       number = number / 10;
     }
     return pow;
   }
 }
}

Output

Enter Number:121
Entered Number 121 is a palindrome number.