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
num = Convert.ToInt32(Console.ReadLine()); // Reading the number entered by the user
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:
Contribute your code and comments through Disqus.
Previous: Write a program in C# Sharp to display the number in reverse order.
Next: Write a program in C# Sharp to find the number and sum of all integer between 100 and 200 which are divisible by 9.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/csharp-exercises/for-loop/csharp-for-loop-exercise-38.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics