C# program to check if a number is Palindromic or not
Here's a C# program to check if a number is palindromic or not:
using System;
class Program
{
static void Main(string[] args)
{
Console.Write("Enter a number: ");
int number = Convert.ToInt32(Console.ReadLine());
if (IsPalindromic(number))
{
Console.WriteLine($"{number} is a palindromic number.");
}
else
{
Console.WriteLine($"{number} is not a palindromic number.");
}
}
static bool IsPalindromic(int number)
{
int originalNumber = number;
int reverse = 0;
while (number > 0)
{
int digit = number % 10;
reverse = (reverse * 10) + digit;
number /= 10;
}
return originalNumber == reverse;
}
}
In this program, the IsPalindromic method takes an integer number as input and checks if it is a palindromic number. It uses a reverse variable to store the reversed number. It iterates through the digits of the original number by continuously dividing it by 10 and extracting the rightmost digit. The reverse number is built by multiplying it by 10 and adding the current digit. Finally, it compares the originalNumber with the reverse number and returns true if they are equal, indicating a palindromic number.
The Main method prompts the user to enter a number, calls the IsPalindromic method with the user input, and displays the appropriate message based on the result.
Note that the program assumes the user will input a positive integer. It doesn't handle exceptions if the input is not a valid positive integer.