Home > OS >  C# how to check for a palindrome number
C# how to check for a palindrome number

Time:06-13

I tried to solve this problem: check if the number is a palindrome, I know that there are a lot of examples on the Internet, but I don't understand why my code isn't working.

using System;
class HelloWorld {
    static void Main() {
        int number = 122;
        int reverse = 0;
        while(number!=0){
            reverse = (reverse*10)   number;
            number/=10;
        }
        Console.Write(reverse);
        if(number==reverse){
            Console.WriteLine("The number is palindrom");
        }
        else{
            Console.WriteLine("The number isn't palindrom");
        }
    }
}

I know that I shouldn't use int number = 122; but this was my way to understand if the code is working.

CodePudding user response:

You should save your original number. In your case number is always 0 when comparing to the reverse number.

using System;
public class HelloWorld {
    public static void Main() {
        int number = 1222111;
        int tmp_number = number;
        int reverse = 0;
        while(tmp_number!=0){
            reverse = (reverse*10)   tmp_number;
            tmp_number/=10;
        }
        Console.Write(reverse);
        if(number == reverse){
            Console.WriteLine(" The number is palindrom");
        }
        else{
            Console.WriteLine(" The number isn't palindrom");
        }
    }
}

CodePudding user response:

Within you while loop you alter the value of number until it's 0. So you always end up comparing

if (0==reverse){

CodePudding user response:

The magic happens in the loop: (reverse*10) "shifts" your result to the next place. number is basically the remainder. With number/=10 you go to the next place until there is nothing left.

It's useful to use the debugger in order to understand what happens.

CodePudding user response:

It would be easier to convert it to a string:

using System;
using System.Linq;
                    
public class Program
{
    public static void Main()
    {
        int number = 1221;
        
        // convert to string, reverse it, join it, parse it to an int
        int reversed = int.Parse( string.Join("", number.ToString().Reverse()));
        
        if(number == reversed)
            Console.WriteLine("The number is palindrom");
        else
            Console.WriteLine("The number isn't palindrom");
    }
}

You don't need to parse it back to an int, but your example uses integers.

CodePudding user response:

This is rather an approach I have followed from general pseudocodes available for multiple programming languages for implementing a palindrome program

class Solution{
public static void Main(string[] args){
   int number = 123;
   if(IsPalindrome(number))
      Console.WriteLine(number " is a palindrome number");
   else
       Console.WriteLine(number " is not a palindrome number");
  }
static bool IsPalindrome(int m){
   int total=0;
   int num=m;
   while(num != 0){
     //get the remainder
     int temp = num;
     total =total  temp*temp*temp;
      //decrement the loop variable
     num=/10;
   }
   //check and return a value to the calling method
    if(total==m)
      return true;
    else
      return false;
  }
}
  • Related