﻿ C# - Check whether a number is a palindrome or not

# C#: Check whether a number is a palindrome or not

## C# Sharp For Loop: Exercise-38 with Solution

Write a program in C# Sharp to check whether a number is a palindrome or not.

Visual Presentation:

Sample Solution:

C# Sharp Code:

``````using System;  // Importing necessary namespace

public class Exercise38  // Declaration of the Exercise38 class
{
public static void Main()  // Main method, entry point of the program
{
int num, r, sum = 0, t;  // Declaration of variables

Console.Write("\n\n");  // Displaying new lines
Console.Write("Check whether a number is a palindrome or not:\n");  // Displaying the purpose of the program
Console.Write("------------------------------------------------\n\n");  // Displaying a separator and new lines

Console.Write("Input a number: ");  // Prompting the user to input a number

t = num;  // Storing the original number

for (; num != 0; num = num / 10)  // Loop to reverse the number
{
r = num % 10;  // Extracting the last digit of the number
sum = sum * 10 + r;  // Reversing the number by storing each digit sequentially
}

if (t == sum)  // Checking if the original number is equal to its reverse
Console.Write("{0} is a palindrome number.\n", t);  // Displaying the result if the number is a palindrome
else
Console.Write("{0} is not a palindrome number.\n", t);  // Displaying the result if the number is not a palindrome
}
}
```
```

Sample Output:

```Check whether a number is a palindrome or not:
------------------------------------------------
Input a number: 8
8 is a palindrome number. ```

Flowchart:

C# Sharp Code Editor: