220k views
5 votes
/* Given a non-negative number "num", return true if num is within 2 of a

* multiple of 10.
*/
public boolean nearTen(int num) num % 10 >= 8;


User Unifreak
by
8.0k points

1 Answer

4 votes

Final answer:

The code checks if a number is within 2 units of a multiple of 10 using a modulus operation in Java, returning true if it is and false otherwise.

Step-by-step explanation:

The question asks how to determine if a given non-negative number, referred to as num, is within 2 units of a multiple of 10. The provided code does this check with a boolean function nearTen in Java. The condition in the function uses the modulo operator % to find the remainder when num is divided by 10. If the remainder is less than or equal to 2 (num % 10 <= 2) or greater than or equal to 8 (num % 10 >= 8), then it means that num is within 2 units of a multiple of 10, and therefore, the function returns true. Otherwise, it returns false.

User Matvs
by
8.1k points