Repository: TheAlgorithms/C-Plus-Plus
File: math/finding_number_of_digits_in_a_number.cpp (C++)

Code before:
        // we can also use `n = n / 10`
        n /= 10;
        // each time the loop is running, `count` will be incremented by 1.
        ++count;
    }

    return count;
}

/**
 * @brief This function finds the number of digits
 * in constant time using logarithmic function
 * TC: O(1)
 * @param n the number to check its digits
 * @returns the digits count
 */
double finding_number_of_digits_in_a_number_using_log(double n) {
    // to handle the negative numbers
    if (n < 0) {
        n = -n;
    }
    // log(0) is undefined
    else if (n == 0) {
        // handling the value 0
        return 0;
    }
    double count = floor(log10(n) + 1);

    return count;
}

/**
 * @brief Self-test implementations
 * @returns void
 */
static void first_test() {
    assert(finding_number_of_digits_in_a_number(5492) == 4);
    assert(finding_number_of_digits_in_a_number(-0) == 0);
    assert(finding_number_of_digits_in_a_number(10000) == 5);
    assert(finding_number_of_digits_in_a_number(9) == 1);
    assert(finding_number_of_digits_in_a_number(100000) == 6);
    assert(finding_number_of_digits_in_a_number(13) == 2);
    assert(finding_number_of_digits_in_a_number(564) == 3);
}

static void second_test() {
    assert(finding_number_of_digits_in_a_number_using_log(5492) == 4);
    assert(finding_number_of_digits_in_a_number_using_log(-0) == 0);
    assert(finding_number_of_digits_in_a_number_using_log(10000) == 5);
    assert(finding_number_of_digits_in_a_number_using_log(9) == 1);
    assert(finding_number_of_digits_in_a_number_using_log(100000) == 6);

Review comment (suggestion):
Use guard if you already know what to return. Looking at your second `if` statement, it's clear that you already know what to `return` so use that as guard instead of `else if`.

 ```suggestion
    if (n == 0) {
        // handling the value 0
        return 0;
    }
    if (n < 0) {
        n = -n;
    }
```

Code after:
        // we can also use `n = n / 10`
        n /= 10;
        // each time the loop is running, `count` will be incremented by 1.
        ++count;
    }

    return count;
}

/**
 * @brief This function finds the number of digits
 * in constant time using logarithmic function
 * TC: O(1)
 * @param n the number to check its digits
 * @returns the digits count
 */
double finding_number_of_digits_in_a_number_using_log(double n) {
    // log(0) is undefined
    if (n == 0) {
        return 0;
    }

    // to handle the negative numbers
    if (n < 0) {
        n = -n;
    }

    double count = floor(log10(n) + 1);

    return count;
}

/**
 * @brief Self-test implementations
 * @returns void
 */
static void first_test() {
    assert(finding_number_of_digits_in_a_number(5492) == 4);
    assert(finding_number_of_digits_in_a_number(-0) == 0);
    assert(finding_number_of_digits_in_a_number(10000) == 5);
    assert(finding_number_of_digits_in_a_number(9) == 1);
    assert(finding_number_of_digits_in_a_number(100000) == 6);
    assert(finding_number_of_digits_in_a_number(13) == 2);
    assert(finding_number_of_digits_in_a_number(564) == 3);
}

static void second_test() {
    assert(finding_number_of_digits_in_a_number_using_log(5492) == 4);
    assert(finding_number_of_digits_in_a_number_using_log(-0) == 0);
    assert(finding_number_of_digits_in_a_number_using_log(10000) == 5);
    assert(finding_number_of_digits_in_a_number_using_log(9) == 1);