[Solved] Checking if a number is a Simber


  1. Your code has compilation errors.

  2. ^ is Binary XOR Operator. You can’t expect it to produce pow(10,i). So replace nmbrs.push_back(((n/10^i) % 10); with nmbrs.push_back(((n/pow(10,i)) % 10);

  3. When I was able to remove the compilation errors, i realized your code has logical errors too. As your given definition of simber, int is_simber(int n) should be fair and simple.

bool is_simber( int n )
{
    if ( n<0 ) return false; // simber is a positive integer

    int digitCount[10] = {}; // initializing all with 0;
    // digitCount array holds the number of occurance of a digit
    // so d[1] holds the number of times 1 occurred
    // d[2] holds the number of times 2 occurred and so on ... 

    while( n ){
        int d = n%10;
        n /= 10;
        digitCount[d]++;
    }

    // we just have to check 
    // any odd digit, if present, occurs an odd number of times

    for( int i=1; i<=9; i= i+2) // Attention i=i+2, to iterate over just odd numbers
    {
        if( digitCount[i] != 0 && digitCount[i]%2 == 0 ) return false;
    }
    return true;
}

4

solved Checking if a number is a Simber