Algorithm for Checking Palindrome
The steps for checking whether the given number is palindrome or not are given below.
- Take the number as user input.
- Put the given number in a temporary variable.
- Reverse the number.
- Start comparing the reversed number with the temporary variable.
- If both the numbers are the same, print that the number is a palindrome.
-
Else print, not a palindrome number.
Palindrome string
How to check if a given number is Palindrome or not in C#?
To check if a number is a palindrome or not in C#, follow these steps:
- Get the user's input.
- Use the ToString() function to convert the number into a string.
- Use the Reverse() function to reverse the string.
- Compare the original string to the string that has been reversed.
- The quantity is a palindrome if the two strings are identical.
- The number is not a palindrome if they are not equal.
Program for Palindrome in C#
Let's look at the palindrome in the C# program. In this program, we will take user input and determine whether or not the number is a palindrome.
using System;
public class PalindromeExample
{
public static void Main(string[] args)
{
int num,rev,sum=0,temp;
Console.Write("Enter the number: ");
num = int.Parse(Console.ReadLine());
temp = num;
while(num>0)
{
rev = num%10;
sum = (sum*10)+rev;
num = num/10;
}
if(temp==sum)
Console.Write("Yay! the given number is Palindrome.");
else
Console.Write("Oops! the given number is not Palindrome.");
}
}
Output:
Enter the number: 121
Yay! the given number is Palindrome.
Enter the number: 197
Oops! the given number is not Palindrome.
Also see, Ienumerable vs Iqueryable
Frequently Asked Questions
What is C#?
C# is a modern, object-oriented programming language that is pronounced "C sharp". C# is one of the languages for the Common Language Infrastructure and is simple for users familiar with C, C++, or Java. It is syntactically very similar to Java.
What is the formula for palindrome in C?
A simple formula to check palindrome is:
for(i=0,j=strlen(str)-1;i<j;i++,j--)
if(str[i]!=str[j])
break;
This compares the first character with the last character, and so on. If any of the characters do not match, the loop breaks, and the string is not a palindrome.
How do you check if a string is a palindrome?
A palindrome is a word that reads the same forwards and backwards. To check if a string is a palindrome, compare its characters from the start and end until the middle, and if all characters match, it is a palindrome.
What are extension methods in C#?
Extension methods enable the addition of new methods to already-existing ones. The new methods are static.
What are Generics in C#?
In C# collections, defining any form of an object is said to be acceptable, violating C#'s fundamental rules of type safety. As a result, generics were used to type-safe the code by allowing the data processing algorithms to be reused.
How to do palindrome in C#?
To check if a word is a palindrome in C#, first, convert it to lowercase for case insensitivity. Then, compare the word with its reverse by iterating over half the word's length. If all characters match from both ends, it's a palindrome; otherwise, it's not.
Conclusion
In this article, we have extensively discussed Palindrome and how to write a program for checking Palindrome in C#. I hope you enjoyed this blog on Palindrome in C#.
Check out this problem - Check If A String Is Palindrome
Also read reverse a number
If you want to learn more, check out our articles on Minimum weight cycle in an undirected graph, Construct the Full K-ary Tree from its Preorder Traversal, Regular and Bipartite graphs, and Planar and Non-Planar Graphs.
Happy Coding!