In this program, You will learn how to check whether number is palindrome or not in C#, using a while loop, or for loop.
A number is a palindrome if the reversed number is equal to the original number. For example 121, 101, 12321 are palindrome numbers.
using System;
class Palindrome {
public static void Main() {
int r,n,rev=0,copy;
Console.Write("Enter the number:");
n=int.Parse(Console.ReadLine());
copy=n;
for(;n>0;n=n/10)
{
// It will find the reverse of the input entered by the user.
rev=rev*10+n%10;
}
// Compare the reverse of input with the temporary variable
if(copy==rev)
Console.Write(copy+" is a palindrome number");
else
Console.Write(copy+" is a not palindrome number");
}
}
using System;
class Palindrome {
public static void Main() {
int r,n,rev=0,copy;
Console.Write("Enter the number:");
n=int.Parse(Console.ReadLine());
copy=n;
while(n>0)
{
// It will find the reverse of the input entered by the user.
rev=rev*10+n%10;
n=n/10;
}
// Compare the reverse of input with the temporary variable
if(copy==rev)
Console.Write(copy+" is a palindrome number");
else
Console.Write(copy+" is a not palindrome number");
}
}
Enter the number:121 121 is a palindrome number
Enter the number:123 123 is a not palindrome number
copy
to check it when we need to compare our original number with reverse numbern%10
and rev*10
will maintain their nth positioncopy
variable