Loops and methods
A for loop and a static method (a function that belongs to the class):
public class Main {
static int square(int n) {
return n * n;
}
public static void main(String[] args) {
for (int i = 1; i <= 3; i++) {
System.out.println(square(i));
}
}
}
Try it
Implement factorial(n) (1 × 2 × … × n) so the program prints:
120