Reputation: 13
I am having problems with my code.
The goal with my code is for a user to enter a number which is then used to form
a multiplication-table.
For example, if the user enters the number 4, the program is supposed to print following:**
1 * 1 = 1
1 * 2 = 2 2 * 2 = 4
1 * 3 = 3 2 * 3 = 6 3 * 3 = 9
1 * 4 = 4 2 * 4 = 8 3 * 4 = 12 4 * 4 = 16
This is my current code. Thankful for any kind of help!
import java.util.Scanner;
public class MultiplicationTable {
public static void main (String[] arg) {
Scanner gt = new Scanner(System.in);
System.out.println("Enter a number for multiplication: ");
int N = gt.nextInt();
for(int i = 1; i < N; i++) {
for(int j = 1; j < N; j++);
int product = i * j;
System.out.print(i +" * " +j +" = " +product );
System.out.println();
}
}
}
Upvotes: 0
Views: 59
Reputation: 11934
There are two tiny errors in your code, otherwise you are good to go.
for(int i = 1; i < N; i++) {
for(int j = 1; j < i; j++); //<-- remove this semicolon
{ // <-- use curly braces here for the loop statements
int product = i * j;
System.out.print(i +" * " +j +" = " +product+" "); //<--add an additional space at the end
}
System.out.println();
}
Upvotes: 1
Reputation: 889
To get that pyramidal structure, you should change the second loop limit to i :
for(int j = 1; j < i; j++)
And remove the semicolon at the end of the loop.
Upvotes: 0