C# Program to Reverse a Number and Check if it is a Palindrome

This is a C# Program to reverse a number & check if it is a palindrome.

Problem Description

This C# Program Reverses a Number & Check if it is a Palindrome.

Problem Solution

Here first it reverses a number. Then it checks if given number and reversed numbers are equal. If they are equal, then its a palindrome.

Program/Source Code

Here is source code of the C# Program to Reverse a Number & Check if it is a Palindrome. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Reverse a Number & Check if it is a Palindrome
 */
using System;
class program
{
    public static void Main()
    {
        int num, temp, remainder, reverse = 0;
        Console.WriteLine("Enter an integer \n");
        num = int.Parse(Console.ReadLine());
        temp = num;
        while (num > 0)
        {
            remainder = num % 10;
            reverse = reverse * 10 + remainder;
            num /= 10;
        }
        Console.WriteLine("Given number is = {0}", temp);
        Console.WriteLine("Its reverse is  = {0}", reverse);
        if (temp == reverse)
            Console.WriteLine("Number is a palindrome \n");
        else
            Console.WriteLine("Number is not a palindrome \n");
        Console.ReadLine();
    }
}
Program Explanation

This C# program we are reading an integer using ‘num’ variable. Compute the modulus of the value of ‘num’ variable by 10 and add the value with the value of ‘reverse’ variable. Divide the value of ‘num’ variable by 10 and assign to ‘num’ variable.

advertisement

If else condition statement is used to check if given number and reversed numbers are equal. If the condition is true then the statement as number is a palindrome. Otherwise, if the condition is false, then print the statement as number is not a palindrome.

Runtime Test Cases
 
Enter an integer
343
Given number is = 343
Its reverse is  = 343
Number is a palindrome

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

Free 30-Day Python Certification Bootcamp is Live. Join Now!
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.