C# Program to Reverse a Number and Check if it is a Palindrome
Posted by Superadmin on August 10 2022 14:35:21

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.

 

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