Creating a diagonal periodic table
Author |
Message |
Krocker
|
Posted: Mon Oct 07, 2013 3:22 pm Post subject: Creating a diagonal periodic table |
|
|
ok so i have to create a diagonal multiplication table, i have managed to make the full table, but unable to get it diagonal.
Something like this (table for 7s)
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49
but i have
1 2 3 4 5 6 7
2 4 6 8 10 12 14
3 6 9 12 15 18 21
4 8 12 16 20 24 28
5 10 15 20 25 30 35
6 12 18 24 30 36 42
7 14 21 28 35 42 49
Heres my code so far:
code: |
import java.util.Scanner;
public class question3{
public static void main (String[] args){
Scanner userInput = new Scanner (System.in); //creates the input object
int factor = 0;
int result;
while (factor < 1 || factor > 10){
System.out.print ("Enter an integer between 1 and 10: ");
factor = userInput.nextInt();
}
// prints main table
for (int i = 1; i <= factor; i++) {
for (int j = 1; j <= factor; j++) {
result = i*j;
System.out.printf("%-5d", result);
}
System.out.println();
}
}
}
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
DemonWasp
|
Posted: Mon Oct 07, 2013 4:24 pm Post subject: RE:Creating a diagonal periodic table |
|
|
Well, on the first row (i == 1) you want the inner loop to print exactly one number.
On the second row (i == 2) you want the inner loop to print exactly two numbers.
Do you see a pattern? How would you implement that pattern? |
|
|
|
|
|
Krocker
|
Posted: Mon Oct 07, 2013 5:13 pm Post subject: RE:Creating a diagonal periodic table |
|
|
ok, so, each row would increase by one, i got that, so # of r== i, but i cant seem to figure out how to imply that to a for loop. The only way i can think of is by using an if statement, however, im only allowed to use for statements. Also, the max number expected is 10, so thats not a problem |
|
|
|
|
|
Raknarg
|
Posted: Mon Oct 07, 2013 7:00 pm Post subject: RE:Creating a diagonal periodic table |
|
|
You just said it yourself, the j loop can only go up to whatever loop i is at. |
|
|
|
|
|
Krocker
|
Posted: Mon Oct 07, 2013 7:28 pm Post subject: RE:Creating a diagonal periodic table |
|
|
ya i figured it out, i made j > row, where row increases by one every time a row is done. It worked! Thanks. |
|
|
|
|
|
|
|