Skip to main content
4 of 5
edited tags
user avatar
user avatar

Return the last digit of a number

public static int lastDigit(int a) {
    a = a % 10;
    if (a <= 0){
    a *= -1;
    }
    return a;
}

This is my solution. The course hasn't taught if else yet, so I'm wondering if there is a way to do it without the if else.

public static int lastDigit(int a) {
a = a % 10;
return a;
}

The problem with this is that it will not print the last digit of a negative number as a negative.

user85459