Monday, 14 September 2020

Program to check a number is Palindrome in C#

In this blog, we will learn How to check a number is Palindrome or not in C#. This Is also one of the most popular interview questions among the interviewers. The basic approach is to reverse the number and then compare it with the number added by the user. 


Let's create the program in C#:

int Number, TmpNumber, Reverse;

Console.WriteLine("Enter a No. to check either it's palindrome or not");

//Get number from the user

Number = int.Parse(Console.ReadLine());

//Hold the number in the TmpNumber

TmpNumber = Number;

Reverse = 0;

//Reverse the Number

while (Number > 0)

{

    int remainder = Number % 10;

    Reverse = (Reverse * 10) + remainder;

    Number = Number / 10;

}

//Compare the Reversed number with the number entered by the user

if (TmpNumber == Reverse)

{

    Console.WriteLine("'{0}' no. is an example of Palindrome", TmpNumber);

}

else

{

    Console.WriteLine("'{0}' no. is not an example of Palindrome", TmpNumber);

}

//Console.ReadLine() to hold the screen after the execution of the program

Console.ReadLine();

Output:

I hope this will help you in your interview preparation/exam.
Thanks

0 comments:

Post a comment

Subscribe Now

Popular Posts

Contact us

Name

Email *

Message *

Subscribe us on YouTube

Like us on Facebook

Follow us on Google+