Menu Close

Palindrome in csharp

In this article we will learn about Palindrome in csharp. Please read our previous article Difference Between IEnumerable, ICollection And IList.

Palindrome in csharp

A Palindromic number or numeral Palindrome is a number, which remains the same when its digits are reversed. Like 16461, for example, it is “symmetrical”. The term palindromic is derived from Palindrome, which refers to a word (such as rotor or racecar or madam) whose spelling is unchanged when its letters are reversed. 

using System;  
  class myFirstWindowsConsoleApp  
   {  
     public static void Main(string[] args)  
      {  
          int n,r,sum=0,temp;    
          Console.Write("Enter the Number: ");   
          n = int.Parse(Console.ReadLine());  
          temp=n;      
          while(n>0)      
          {      
           r=n%10;      
           sum=(sum*10)+r;      
           n=n/10;      
          }      
          if(temp==sum)      
             Console.Write("Number is Palindrome.");      
          else      
             Console.Write("Number is not Palindrome");     
         Console.ReadKey();
    }  
  }  

Output:

Enter the Number=141   
Number is Palindrome.
Enter the number=143  
Number is not Palindrome.

Palindrome in C#

Palindrome number algorithm

Get the number from user

Hold the number in temporary variable

Reverse the number

Compare the temporary number with reversed number

If both numbers are same, print palindrome number

Else print not palindrome number

References

docs.microsoft.com/en-us/samples/browse/

Conclusion

In this article we will learnt about Palindrome.

1 Comment

Leave a Reply

Your email address will not be published.