Find the largest palindrome made from the product of two 3-digit numbers. (Java)

后端 未结 4 2036
难免孤独
难免孤独 2021-01-16 20:21

I am having a bit of trouble solving Project Euler question 4. I am inexperienced in programming and didn\'t really understand other answers. I managed to write a code that

4条回答
  •  无人共我
    2021-01-16 20:49

    Here is a code to check PALINDROME-ness of a number without converting it to a String.

    bool checkPalindrome(int n)
    {
        int num = n;
        int s = 0;
        while(num!=0)
        {
          s = s*10 + (num%10);
          num = num/10;
        }
        if(s==n)
          return true;
        return false;
    }
    

    As you can see that if n = 120, the reverse is calculated as s = 21 instead of 021, but that is fine, keeping in mind that a palindrome number won't end in 0, because if it does, then it would have to begin with 0 which makes it an invalid number!!!!

    Hope this helps!!!

提交回复
热议问题