Palindrome program in C#

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Palindrome program in C#

A palindrome number is a number that is same after reverse. For example 121, 34543, 343, 131, 48984 are the palindrome numbers.

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

Let's see the palindrome program in C#. In this program, we will get an input from the user and check whether number is palindrome or not.

 
  1. using System;  
  2.   public class PalindromeExample  
  3.    {  
  4.      public static void Main(string[] args)  
  5.       {  
  6.           int n,r,sum=0,temp;    
  7.           Console.Write("Enter the Number: ");   
  8.           n = int.Parse(Console.ReadLine());  
  9.           temp=n;      
  10.           while(n>0)      
  11.           {      
  12.            r=n%10;      
  13.            sum=(sum*10)+r;      
  14.            n=n/10;      
  15.           }      
  16.           if(temp==sum)      
  17.            Console.Write("Number is Palindrome.");      
  18.           else      
  19.            Console.Write("Number is not Palindrome");     
  20.     }  
  21.   }  

Output:

Enter the Number=121   
Number is Palindrome.
Enter the number=113  
Number is not Palindrome.