0

Possible Duplicate:
How do I check if a number is a palindrome?

Hello All i want to make program to check whether palindrome number or not when user input the number. But my work does not work at all... Can you guys help me...

class Program
    {
        static void Main(string[] args)
        {
            int i = 0, j = 0 ;

            int numbers =Convert.ToInt32( Console.ReadLine());
            i = numbers % 10;
            do
            {
               j = numbers / 10;

            }
            while (j < 10);

            if (i == j)
            {
                Console.WriteLine(" this is palindrome number");
            }
            else
            {
                Console.WriteLine("not a palindrome");
      }
Community
  • 1
  • 1

6 Answers6

4

The quickest way is to reverse the string and compare it to the original. You don't really need the integer conversion.

You may want to filter or correct the user's input by stripping leading zeroes (i.e. in 010). For example : string number = Convert.ToInt32(Console.ReadLine()).ToString();

slaphappy
  • 6,894
  • 3
  • 34
  • 59
0

You don't have to convert it to integer. You can check it from the string.

Take the first character and the last character compare those .

Iterate the first pointer +1 and the last pointer -1 then compare.

Continue this process upto you are in middle of the string.

  • 4
    although if this is [tag:homework] then it probably needs to be done with a number. – George Duckett Aug 05 '11 at 09:09
  • 1
    So 10 is a numeric palindrome of 1? – Sebastian Mach Aug 05 '11 at 09:13
  • @Carra: Needs to check for special case "0" first. – Sebastian Mach Aug 05 '11 at 09:31
  • @phresnel I don’t understand your (repeated) comment. How is this an issue in this suggested solution? Reversing the string `"10"` yields `"01"` which can be directly compared to the original string. I don’t particularly like the string comparison solution but your objection is misguided. – Konrad Rudolph Aug 05 '11 at 09:49
  • 1
    @Konrad Rudolph: If the input is "01", which is accepted as the integer 1 in the countries I know, then using just string reversing and then comparison would tell that "1" is not a numeric palindrome of "01". – Sebastian Mach Aug 05 '11 at 10:10
  • @Konrad Rudolph: I realise my comments miss a trailing zero. Should have been "So 10 is a valid palindrome of 10?" (see "010"). – Sebastian Mach Aug 05 '11 at 10:12
0

a disscussion

palindrome check

C# code to check palindrome

You can follow these links .

#include<stdio.h>
#include<math.h>
void main()
{
     long int n, num, rev = 0, dig;
     clrscr();
     printf("\n\n\t ENTER A NUMBER...: ");
     scanf("%ld", &num);
     n = num;
     while(num>0)
     {
          dig = num % 10;
          rev = rev * 10 + dig;
          num = num / 10;
     }
     if (n == rev)
           printf("\n\t GIVEN NUMBER IS A PALINDROME");
     else
           printf("\n\t GIVEN NUMBER NOT A PALINDROME");
     getch();
}
  • 2
    Sorry, this is not how we do things here on StackOverflow. Posting a link to a Google search **that doesn't work** but would supposedly *find a piece of code that does this* is hardly an helpful answer. – R. Martinho Fernandes Aug 05 '11 at 09:36
  • But those links can be helpful. –  Aug 05 '11 at 15:38
  • What links? All I get from that link is a blank page (and I checked, I'm not the only one). And you could post one of those *helpful links* instead. Or post some helpful content derived from them. – R. Martinho Fernandes Aug 05 '11 at 15:39
  • ops, there was some problem pasting that link, i have edited that post. –  Aug 05 '11 at 16:10
0

First of all by converting the digits to an Int32, the number of digits a user can enter are limited.

By using the modulo operation, the i variable will contain the last digit of the entered number.

By dividing the number by 10, j should eventually contain the first digit entered. However you aren't dividing it "until j is smaller than 10", but you're dividing it "while j is smaller than 10", effectively making the contents of j depend on the number of digits entered.

Even if you would fix the while condition, this code will only check the first and last digit, making it function only for 1, 2 and 3 digit numbers.

C.Evenhuis
  • 25,996
  • 2
  • 58
  • 72
0

You'll need something like this:

        bool isPalindrome = true;
        string s = "300212003";
        for (int i = 0; i < (s.Length / 2); i++)
        {
            if (s[i] != s[s.Length - i - 1])
            {
                isPalindrome = false;
                break;
            }
        }
        Console.WriteLine(isPalindrome);

It's faster than reverting a string and comparing it, only need O(n/2) operations.

Carra
  • 17,808
  • 7
  • 62
  • 75
-1

Something is palindrome when you can read it either way, so when the reverse is the same as the original. So just reverse your input string and compare it to the original.

TJHeuvel
  • 12,403
  • 4
  • 37
  • 46