Lab 3. Solutions

Task 1. Is even.

bool is_even(int n) {
    return n % 2 == 0;
}

Task 2. Sum of digits

int num_of_digits(int n) {
    n = abs(n);
    if (n == 0)
        return 1;
    else {
        int d = 0;
        while (n > 0) {
            n = n/10;
            d++;
        }
        return d;
    }
}

Task 3. First and last digits

bool first_equals_last(int n) {
    return first(n) == last(n);
}

int first(int n) {
    while (n / 10 > 0) {
        n = n / 10;
    }
    return n;
}

int last(int n) {
    return n % 10;
}