.
Goal is for user to input 2 numbers then get a multiplication chart with a row and column.
Example: user enters 3 and 5, then should print
...3 4 5 (disregard the dots)
3 9 12 15
4 12 16 20
5 15 20 25

My program will not correctly put the chart next to the column but the chart itself multiplies correctly. any suggestions?

import java.util.*;

class ForLoop1{
public static void main (String [] args){

Scanner scan = new Scanner(System.in);

System.out.println();
System.out.println("Multiplication Table");
System.out.println();
System.out.println("Enter Values");

int a = scan.nextInt();
int b = scan.nextInt();

if(a>b){
int c = a;
int d = b;
a = d;
b = c;
}

for(int q=a;q<=b;q++){

System.out.print("\t"+q+"\t");

}
for(int q=a;q<=b;q++){

System.out.print("\n"+q);
}
System.out.println();

for(int i = a; i<=b; i++){
for(int j = a; j<=b; j++){

System.out.print("\t"+i*j+"\t");

}
System.out.println("\n");

}
}
}

## All 8 Replies

i would say merge your second row loop with the third double loop that prints the values. i.e. when you go to print your firt row of values, before the first print , print the row number, then print each value. ie..e if values are 3 9 12 15, print row value 3 then 3 9 12 and 15 then print a line feed and do it again. Also for alignment when printing the top column, maybe add some spaces so you know each column number at top is a field of 2 or 3 length.

A way to have something print on every row is

``````for(int i = a; i<=b; i++){
// print row edit: changes this to properly row
for(int j = a; j<=b; j++){

System.out.print("\t"+i*j+"\t");

}
System.out.println("\n"); // you are correctly printing out line feed here

}``````

make sure you get a \n in after you print your first row of column headers.
Mike

lets say you had a simple double loop like

``````for(int i=1; i<10; i++)
{ for(int j=1; j<10; j++)
{
System.out.print("" + i*j);// you may be able to just print i*j, i know when they want to print a string i start with an empty "" before concatenating a number.
}// end j loop
}// end i loop``````

the numbers can be from 1 to 81 1* 1 and 9*9.

Lets say you wanted each field to be 3 spaces. well 1 or 2 or 3 is two spaces short.

11 or 33 or any of your two digit numers is one space short.

``````for(int i=1; i<10; i++)
{ for(int j=1; j<10; j++)
{
System.out.print("" + i*j);
printSpaces(i*j);
}// end j loop
}// end i loop``````

so what does print spaces do? it prints either 1 or 2 spaces ' ', depending on if the number is < 10 or not. It just checks the number and prints the spaces. That's a method you have to write. Conceivably every number you print in this program can be run through print spaces. if your numbers can be 100 or more you have 3 checks. you check for less than 10 and print 2 spaces, for between 10 and 99 you print one space and numbers 100 or more you don't print any spaces. Keeps things lined up.

Mike

I still can't get the column to properly align. If i deleted the

``````      for(int q=a;q<=b;q++){

System.out.print("\n"+q);
``````

then the column is removed and appears as

``````3 4 5
9 12 15
12 16 20
15 18 21
``````

What i'm having a hard time figuring out is how to insert the column within the double loop with it only printing out once. Appreciate the help so far.

if you have two loops, outer loop is your rows, inner loop is each item on the row or column, then

``````for(int i=1; i<10; i++)  // row loop
{

// what prints here prints once per row. print the row number
print i; // little pseudo code

// now inner loop. print each item on row
for(j=0; j<10; j++)
{print j;
} // this populates the columns in the row.  you are printing 10 things directly after the row number

print "\n"; // now we are out of the inner loop and once again in outer loop. so printing a carriage return, this happens once per row.

}``````

I used my pseudo code print and i didn't bother printing any spaces or tabs but that is the idea behind where stuff goes to be printed in the two loops. You already got the idea of printing something once per row with your "\n" you print after each execution of the inner loop, that prints only once for each iteration of the outer loop. you essentially are doing the same thing with row numbers except you print it before the iteration of the inner loop.

so flow is this

outer loop:

print row number

innerloop: print all values on row

print carriage return

end outer loop

Mike

Also the first row is your column number. print that first. Don't forget that your columns start at the second index along the row. so maybe for the first print , print *, as a place holder.

i.e
* 3 4 5
3 9 12 15
4 12 16 20

now your numbers wont look really lined up unless you use the print space code i suggested.

Mike

[edit just for clarification your very first printing of the first line is not going to be in the double loop described above. you will print that first, being sure to indent the start of printing since your first number is actually the second item in any of your generic rows. then run the double loop and print the row numbers as mentioned within the double loop.

``````for(int q=a;q<=b;q++){

System.out.print("\t"+q+"\t");

}

for(int q=a;q<=b;q++){

System.out.print("\n"+q);

for(int i = a; i<=b; i++){
for(int j = a; j<=b; j++){

System.out.print("\t"+i*j+"\t");
}
}

System.out.println("\n");
}

}
}
``````

i tried that and it still didn't work. where did i go wrong there

I never suggested a triple loop. stage one print your first row which is your column headers. ok done with that print \n. stage 2 is the double loop i described. stage two prints row number then number * number tell your done then \n and then you iterate your outer loop again for next row.

Adam. You are my Savior. Thanks for your patience i'm a noob

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.