You must Sign In to post a response.
  • Category: .NET

    How to develop Palindrome program in c sharp


    Are you looking for a way to develop Palindrome program in c sharp ? then read this thread to know how to develop it



    i have used palindrome program in dotnet with string value can we use with int value. please guide me.
    like
    121 result palindrome
    123 result not palindrome
  • #743024

    -----------------------------------------------------------------------------
    Regards,
    Gopi A.
    +91 9894315571
    Skype:gopi.net
    http://asaigopi-dotnet.blogspot.in/

  • #743044

    see below code snippet, which gives you palindrome

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace Palindromeprogram
    {
    class Program
    {
    static void Main(string[] args)
    {
    string str = string.Empty;
    Console.WriteLine("Enter a String");

    string s = Console.ReadLine();

    int i = s.Length;
    //we can get the Length of string by using Length Property

    for (int j = i - 1; j >= 0; j--)
    {
    str = str + s[j];
    }
    if (str == s)
    {

    Console.WriteLine(s + " is palindrome");

    }
    else
    {
    Console.WriteLine(s + " is not a palindeome");
    }

    Console.WriteLine(str);

    Console.ReadLine();
    }
    }
    }



    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #743057
    Simply use the Inbuilt methods to check the string reverse is same or not




    string myStr = "ANINA";

    return myStr.SequenceEqual(myStr.Reverse());

    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #743140
    Hi,
    This is the easiest algorithm which you use

    public boolean isPalindrom(Integer number){
    char[] n1 = number.toString().toCharArray();
    char[] n2 = Array.reverse(n1);

    String s1 = new String(n1);
    String s2 = new String(n2);

    return (s1 == s2);

    }
    As it is an Integer you can also use for s1 and s2 and return (s1-s2 == 0)

    Otherwise we can also use the following algiorithm which is popularly used for integer

    n = num;
    rev = 0;
    while (num > 0)
    {
    dig = num % 10;
    rev = rev * 10 + dig;
    num = num / 10;
    }
    If n == rev then num is a palindrome


  • Sign In to post your comments