Methods
Static methods: parameters, return values, GCD and a primality check.
Methods in Java are the equivalent of functions. In contest solutions they are declared static, next to main: then they can be called directly, without creating objects.
Below is Euclid's algorithm for the greatest common divisor.
Java
GCD of two numbers. Enter: 12 8
Methods returning boolean are convenient for checks. When testing primality, try divisors only up to the square root of n (the condition i * i <= n) — that cuts the work from n steps down to the square root of n.
Java
Primality check up to the square root. Enter: 97
The last println uses the ternary operator: condition ? value_if_yes : value_if_no — a compact if/else for expressions.
Task: write a method long digitSum(long n) that returns the sum of a number's digits.
Then solve the attached problems "GCD of Two Numbers" and "Prime Check" — and mark the lesson as completed.