#Making a multiplication table without using any loops
1 messages Β· Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
Here is an AI assisted attempt to answer your question π€. Maybe it helps! In any case, a human is on the way π. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
If you want to create a multiplication table without using any loops, you can use recursion instead. Here's an example of how you can achieve this:
public class MultiplicationTable {
public static void main(String[] args) {
printMultiplicationTable(1, 1);
}
public static void printMultiplicationTable(int row, int col) {
if (row <= 10) {
if (col <= 10) {
System.out.printf("%4d", row * col);
printMultiplicationTable(row, col + 1);
} else {
System.out.println();
printMultiplicationTable(row + 1, 1);
}
}
}
}
In this example, the printMultiplicationTable method is called recursively to print each element of the multiplication table. The row and col parameters represent the current row and column numbers.
The base case for recursion is when row becomes greater than 10. At that point, the method stops calling itself and the program terminates.
Note that this approach is not recommended for large multiplication tables as it may lead to a StackOverflowError due to excessive recursion.
I hope the chatgpt answer helps?
you could use an array to create the table by hand. otherwise, recursion would be the alternative to using loops
I wouldn't use a stream since given the format they're likely supposed to showcase they understand recursion.
I mean, it's not specified? π
im actually confused how would you do it with array??
this was the task
you would hard-code the array, although it does sound like this is a recursion assignment