java multiplication table code example
Example 1: Write a program that prints a multiplication table for numbers up to 12.
public class Main {
public static void main(String[] args) {
for (int y = 1; y <= 12; ++y) {
for (int x = 1; x <= 12; ++x) {
System.out.printf("%4d", y*x);
}
System.out.println();
}
}
}
Example 2: Multiplication table for number Java
public static String multiTable(int num) {
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= 10; i++) {
int result = i * num;
sb.append(i + " * " + num + " = " + result + "\n");
}
return sb.toString().trim();
}