• Home
  • About
  • Contact
  • ado.net
  • angular
  • c#.net
  • design patterns
  • linq
  • mvc
  • .net core
    • .Net Core MVC
    • Blazor Tutorials
  • sql
  • web api
  • dotnet
    • SOLID Principles
    • Entity Framework
    • C#.NET Programs and Algorithms
  • Others
    • C# Interview Questions
    • SQL Server Questions
    • ASP.NET Questions
    • MVC Questions
    • Web API Questions
    • .Net Core Questions
    • Data Structures and Algorithms

Monday, August 10, 2020

Palindrome Program in C# with Examples

 Admin     August 10, 2020     C#, Interview Question, Programming, Programs     No comments   

In this article, I am going to discuss the Palindrome Program in C# with some examples. Please read our previous article where we discussed the Prime Number Program with some examples. This is one of the interview questions asked in the interview to write the logic to check whether a number or string is a palindrome or not. As part of this article, we are going to discuss the following pointers.
  1. What is Palindrome?
  2. How to check if a given number is Palindrome or not?
  3. How to check if a given string is Palindrome or not?
Palindrome Number:
A Palindrome number is a number that is going to be the same after reversing the digits of that number. For example, the numbers such as 121, 232, 12344321, 34543, 98789, etc. are the palindrome numbers.

How to check if a given number is Palindrome or not?
Algorithm to check Palindrome Number in C#:
  1. First, get the number from the user which you check
  2. Hold that number in a temporary variable
  3. Reverse that number
  4. Compare the temporary number with the reversed number
  5. If both numbers are same, then print it is palindrome number else print it is not a palindrome number
Let us implement the above algorithm with a program in C# to check whether a number is a Palindrome or not.
using System;
namespace LogicalPrograms
{
    public class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter a Number To Check Palindrome : ");
            int number = int.Parse(Console.ReadLine());
            int remineder, sum = 0;
            int temp = number;
            while (number > 0)
            {
                //Get the remainder by dividing the number with 10  
                remineder = number % 10;
                //multiply the sum with 10 and then add the remainder
                sum = (sum * 10) + remineder;
                //Get the quotient by dividing the number with 10 
                number = number / 10; 
            }
            if (temp == sum)
            {
                Console.WriteLine($"Number {temp} is Palindrome.");
            }
            else
            {
                Console.WriteLine($"Number {temp} is not Palindrome");
            }
            Console.ReadKey();
        }
    }
}
Output:
How to check if a given number is Palindrome or not?

How to check if a given string is Palindrome or not in C#?
In the following program, we take the string as an input from the console. Then we reverse the string using for loop and storing the reverse string value in the reverse variable. Finally, we check whether the original and reverse values are the same or not. If both are same then the string is Palindrome else it is not Palindrome.
using System;
namespace LogicalPrograms
{
    public class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter a string to Check Palindrome : ");
            string name = Console.ReadLine();
            string reverse = string.Empty;
            
            for (int i = name.Length - 1; i >= 0; i--)
            {
                reverse += name[i];
            }
            
            if (name == reverse)
            {
                Console.WriteLine($"{name} is Palindrome.");
            }
            else
            {
                Console.WriteLine($"{name} is not Palindrome");
            }
            Console.ReadKey();
        }
    }
}
Output:
How to check if a given string is Palindrome or not in C#

Using the for-each loop:
Let us see how to do the previous program using a for each loop.
using System;
namespace LogicalPrograms
{
    public class Program
    {
        static void Main()
        {
            Console.Write("Enter a string to Check Palindrome : ");
            string name = Console.ReadLine();
            string reverse = string.Empty;
            foreach (char c in name)
            {
                reverse = c + reverse;
            }
            if (name.Equals(reverse, StringComparison.OrdinalIgnoreCase))
            {
                Console.WriteLine($"{name} is Palindrome");
            }
            else
            {
                Console.WriteLine($"{name} is not Palindrome");
            }
            Console.ReadKey();
        }
    }
}
Output:
How to check if a given string is Palindrome or not using Foreach Loop

Another Approach:
In the following example, first, we convert the string to a character array. Then using the Array class Reverse method we are reversing the elements of the character array. Once we reverse the elements of the character array, then we create a string from this array. Finally, we are comparing this newly created string with the original string and printing it is Palindrome if both the strings are the same else we are just printing it is not Palindrome.
using System;
namespace LogicalPrograms
{
    public class Program
    {
        static void Main()
        {
            Console.Write("Enter a string to Check Palindrome : ");
            string name = Console.ReadLine();
            char[] nameArray = name.ToCharArray();
            Array.Reverse(nameArray);
            string reverse = new string(nameArray);
            
            if (name.Equals(reverse, StringComparison.OrdinalIgnoreCase))
            {
                Console.WriteLine($"{name} is Palindrome");
            }
            else
            {
                Console.WriteLine($"{name} is not Palindrome");
            }
            Console.ReadKey();
        }
    }
}
Output:
Palindrome Program in C# with Examples

That’s it for today. Here, in this article, I try to explain the different ways to check whether a number or a string is Palindrome or not using C#. In the next article, I am going to discuss how to reverse a number and a string in C# with some examples.

Summary:
I hope this post will be helpful to write a Palindrome Program in C# with Examples
Please share this post with your friends and colleagues.
For any queries please post a comment below.
Happy Coding 😉
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Newer Post Older Post

0 comments:

Post a Comment

If you like this website, please share with your friends on Facebook, Twitter, LinkedIn.

Join us on Telegram

Loved Our Blog Posts? Subscribe To Get Updates Directly To Your Inbox

Like us on Facebook

Popular Posts

  • What is Dependency Injection(DI)
    Hi friends! Today we are going to learn about Dependency Injection and in our last session we have come across Static classes and where it s...
  • C# Programming Examples on Sorting
    Today i am going to tell you some of the Sorting programming questions in C#. Q1- Write a C# program to perform Selection sort. Ans:  Sel...
  • Calling Web API Service in a Cross-Domain Using jQuery AJAX
    In this article, I am going to discuss Calling Web API Service in a Cross-Domain Using jQuery AJAX . Please read our previous article befor...
  • ViewBag in ASP.NET Core MVC
    In this article, I am going to discuss the use of ViewBag in ASP.NET Core MVC application with examples. Please read our previous article ...
  • Recursion And Back Tracking
    In this article, I am going to discuss Recursion And BackTracking in detail. Please read our previous article where we discussed Master Th...
  • What is Abstract Class and When we should use Abstract Class
    Hi friends! In our previous sessions we have seen  Difference Between Class and Struct . And in our last session  we learnt Usability of Sec...
  • Binary to Decimal Conversion in C# with Examples
    In this article, I am going to discuss the Binary to Decimal Conversion in C# with some examples. Please read our previous article where w...

Blog Archive

Contact Form

Name

Email *

Message *

Tags

.Net .Net Core .Net Core MVC Algorithm Angular Anonymous Types Asp.Net Asp.Net MVC Blazor C# Data Structure Database Design Patterns Entity Framework Entity Framework Core Filters Interview Question Management Studio Programming Programs SQL Server SSMS Web API

Copyright © C# Techtics | All Right Reserved.

Protected by Copyscape