Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Palindrome
3.
Algorithm for Checking Palindrome
4.
How to check if a given number is Palindrome or not in C#?
5.
Program for Palindrome in C#
6.
Frequently Asked Questions
6.1.
What is C#?
6.2.
What is the formula for palindrome in C?
6.3.
How do you check if a string is a palindrome? 
6.4.
What are extension methods in C#?
6.5.
What are Generics in C#?
6.6.
How to do palindrome in C#? 
7.
Conclusion
Last Updated: Jun 10, 2024
Easy

Palindrome Program in C#

Author Rajat Agrawal
0 upvote
palindrome in C#

Introduction

C# is a modern, object-oriented programming language that is pronounced "C sharp". It was created by Microsoft under the direction of Anders Hejlsberg and his team as part of the .Net program, and the European Computer Manufacturers Association (ECMA) and International Standards Organization (ISO) both gave their approval. 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.

In this blog, we will discuss how to write a program for checking palindrome in C#.

What is Palindrome

A palindrome number is one that remains the same after being reversed. For example, the palindrome numbers are 121, 34543, 343, 131, 191, 1221, 48984, etc.

palindrome

Also read palindrome number in python.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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 graphConstruct the Full K-ary Tree from its Preorder TraversalRegular and Bipartite graphs, and Planar and Non-Planar Graphs.

Happy Coding!

Previous article
Fibonacci Series in C#
Next article
How to Convert String to DateTime in C#
Live masterclass