c-sharp
  1. c-sharp-program-to-check-whether-a-number-is-palindrome-or-not

Program to Check Whether a Number is Palindrome or Not - (C# Basic Programs)

A palindrome number is a number that remains the same when its digits are reversed. For example, 121, 24542, 7777777 are all palindrome numbers. In this tutorial, we'll discuss a program to check whether a number is a palindrome or not using C# programming language.

Syntax

The syntax for checking whether a number is a palindrome or not in C# is as follows:

int number, reversedNumber = 0, remainder, originalNumber;
Console.WriteLine("Enter an integer: ");
number = int.Parse(Console.ReadLine());
originalNumber = number;

while (number > 0) {
    remainder = number % 10;
    reversedNumber = reversedNumber * 10 + remainder;
    number /= 10;
}

if (originalNumber == reversedNumber) {
    Console.WriteLine(originalNumber + " is a palindrome number");
} else {
    Console.WriteLine(originalNumber + " is not a palindrome number");
}

Example

using System;

namespace PalindromeNumberExample {
    class Program {
        static void Main(string[] args) {
            int number, reversedNumber = 0, remainder, originalNumber;
            Console.WriteLine("Enter an integer: ");
            number = int.Parse(Console.ReadLine());
            originalNumber = number;

            while (number > 0) {
                remainder = number % 10;
                reversedNumber = reversedNumber * 10 + remainder;
                number /= 10;
            }

            if (originalNumber == reversedNumber) {
                Console.WriteLine(originalNumber + " is a palindrome number");
            } else {
                Console.WriteLine(originalNumber + " is not a palindrome number");
            }
            Console.ReadKey();
        }
    }
}

Output:

Enter an integer: 12321
12321 is a palindrome number

Explanation

In the above program, we first take input from the user. We then store the original number in a variable called originalNumber for later comparison. We use a while loop to reverse the number and store it in a variable called reversedNumber. The loop continues until the number is greater than 0. The reversedNumber is calculated by taking the remainder of the number divided by 10 (which gives us the last digit of the number), multiplying reversedNumber by 10, and adding the remainder. Once the loop is complete, we compare the originalNumber and reversedNumber to determine whether the originalNumber is a palindrome or not.

Use

This program is useful in applications that require checking whether a given number is a palindrome or not. It can be used in a variety of programming scenarios, such as in financial applications, or in gaming applications where numbers are used to represent scores or levels.

Summary

In this tutorial, we discussed a program to check whether a number is a palindrome or not using C# programming language. We have seen the syntax, example, explanation, and use of checking whether a number is a palindrome or not in C#. By practicing this program, developers can improve their understanding of basic programming concepts and develop more robust applications.

Published on: