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
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.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

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
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.