Question

I've tried to check whether a number is a palindrome with the following code:

unsigned short digitsof (unsigned int x)
{
    unsigned short n = 0;
    while (x)
    {
        x /= 10;
        n++;
    }
    return n;
}

bool ispalindrome (unsigned int x)
{
    unsigned short digits = digitsof (x);

    for (unsigned short i = 1; i <= digits / 2; i++)
    {
        if (x % (unsigned int)pow (10, i) != x % (unsigned int)pow (10, digits - 1 + i))
        {
            return false;
        }
    }
    return true;
}

However, the following code isn't able to check for palindromes - false is always returned even if the number is a palindrome.

Can anyone point out the error?

(Please note: I'm not interested to make it into a string and reverse it to see where the problem is: rather, I'm interested to know where the error is in the above code.)

Était-ce utile?

La solution 2

The problem is this:

x % (unsigned int)pow (10, i)

Lets try:

x =504405
i =3

SO I want 4.

x % 10^3 => 504405 %1000 => 405 NOT 4

How about

x / (unsigned int)pow (10, i -1) % 10

Autres conseils

I personally would just build a string from the number, and then treat it as a normal palindrome check (check that each character in the first half matches the ones at length()-index).

x % (unsigned int)pow (10, i) is not the ith digit.

Just for more info! The following two functions are working for me:

double digitsof (double x)
{
    double n = 0;
    while (x > 1)
    {       
        x /= 10;
        n++;
    }
    return n;
}

bool ispalindrome (double x)
{
    double digits = digitsof (x);
    double temp = x;
    for(double i = 1; i <= digits/2; i++)
    {
        float y = (int)temp % 10;
        cout<<y<<endl;
        temp = temp/10;

        float z = (int)x / (int)pow(10 , digits - i);
        cout<<(int)z<<endl;
        x = (int)x % (int)pow(10 , digits - i);

        if(y != z)
            return false;

    }
    return true;        
}

Code to check if given number is palindrome or not in JAVA

import java.util.*;
public class HelloWorld{

private static int countDigits(int num) {
    int count = 0;
    while(num>0) {
        count++;
        num /= 10;
    }
    return count;
}
public static boolean isPalin(int num) {
    int digs = HelloWorld.countDigits(num);
    int divderToFindMSD = 1;
    int divderToFindLSD = 1;

    for (int i = 0; i< digs -1; i++)
    divderToFindMSD *= 10;

    int mid = digs/2;  

    while(mid-- != 0)
    {
        int msd = (num/divderToFindMSD)%10;
        int lsd = (num/divderToFindLSD)%10;
        if(msd!=lsd)
        return false;

        divderToFindMSD /= 10;
        divderToFindLSD *= 10;
    }
    return true;

}

 public static void main(String []args) {
    boolean isPalin = HelloWorld.isPalin(1221);
    System.out.println("Results: " + isPalin);
 }
 }

I have done this with my own solution which is restricted with these conditions

  1. Do not convert int to string.
  2. Do not use any helper function.
var inputNumber = 10801

var firstDigit = 0

var lastDigit = 0

var quotient = inputNumber



while inputNumber > 0 {

    lastDigit = inputNumber % 10

    var tempNum = inputNumber

    var count = 0

    while tempNum > 0 {

        tempNum = tempNum / 10

        count = count + 1

    }

    var n = 1

    for _ in 1 ..< count {

        n = n * 10

    }

    firstDigit = quotient / n

    if firstDigit != lastDigit {

        print("Not a palindrome :( ")

        break

    }

    quotient = quotient % n

    inputNumber = inputNumber / 10

}

if firstDigit == lastDigit {

    print("It's a palindrome :D :D ")

}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top