Results 1 to 18 of 18
Thread: Multiplication Table
- 12-24-2010, 12:54 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 22
- Rep Power
- 0
Multiplication Table
I am new to arrays and working on a multiplication table. The code below works fine and displays as follows:
1 2 3
2 4 6
3 6 9
I have been trying to have the output look a bit different with no luck.
I would like the output to look like this:
0| 1 2 3 //I would like the zero space and | to be a blank space in this line
1| 1 2 3
2| 2 4 6
My code:
Java Code:/*MultiplyTbl * */ import java.util.*; public class MultTable { public static void main(String[] args) { // declare variables int[][] table = new int[13][13]; //max 12 for table Scanner question = new Scanner(System.in); System.out.println("Enter 1 for full table OR 2 for specific number:"); int ans = question.nextInt(); if (ans==1) { //generate the full multiplication table for(int row = 1; row < 13; row++) //for rows starting at number { for(int col = 1; col < 13; col++) //for columns { table[row][col] = row * col; System.out.print(table[row][col] + "\t"); }// end for col System.out.println(); }// end row }// end if System.out.println(); //an else statement goes here but not necessary for solution }//end main }//end classLast edited by Eranga; 12-24-2010 at 01:09 AM. Reason: code tags added
- 12-24-2010, 01:08 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
where to add that?Java Code:System.out.print((row - 1) + " | ");
- 12-24-2010, 01:09 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
By the way, please use code tags next time. Unformated codes are really hard to read. If you don't know how to do that, check on my forum signature. :)
- 12-24-2010, 01:12 AM #4
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 3
Hey Billy.
Can you be a little more clear on what you are trying to accomplish?
- 12-24-2010, 01:18 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
With the description at the top, he's clear with what he wants to do, isn't it. ;)
- 12-24-2010, 01:33 AM #6
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 3
- 12-24-2010, 01:40 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
That gives you the whole picture.I would like the output to look like this:
- 12-24-2010, 02:10 AM #8
Member
- Join Date
- Nov 2010
- Posts
- 22
- Rep Power
- 0
Clarification
What I would like to try and do is have the first row/column table[0][0] to be blank...no thing there. The result would look like and Excel worksheet with the numbers starting in the second column of the first row and the second row of the first column. I would like the | separator to only show following the numbers in column[0] in the second row. Maybe this will help:
blank 1 2 3
1| 1 2 3
2| 2 4 6
3| 3 6 9
Thank you.
- 12-24-2010, 02:50 AM #9
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 3
......... Edit
Last edited by AcousticBruce; 12-24-2010 at 02:58 AM.
- 12-24-2010, 02:57 AM #10
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 3
I had a class open called Sum2Power and used that to test out this code. So don't mind the name.
I added a few extra " " spaces in the System.out.print codes.
If you have a different font for your output and it is skewed. Just delete the extra " " spaces in the code.
Here is my idea.
Java Code:public class Sum2Power { public static void main(String[] args) { int[][] tTable = new int[13][13]; for (int row = 0; row < 13; row++) { for (int col = 0; col < 13; col++) { if (col == 1) { if (row != 0) { System.out.print("| "); // I added 2 spaces after "|" to make it look better. } else { System.out.print(" "); // I added 2 more spaces to " " as I did above. Otherwise it skews. } } tTable[row][col] = (row + 1) * (col ); if (row*col == 0) { tTable[row][0] = row; } if (tTable[row][col] == 0) { System.out.print(" "); //added 3 " " spaces here. } else { System.out.print(tTable[row][col] + "\t"); } } System.out.println(); }
My output was
Java Code:1 2 3 4 5 6 7 8 9 10 11 12 1 | 2 4 6 8 10 12 14 16 18 20 22 24 2 | 3 6 9 12 15 18 21 24 27 30 33 36 3 | 4 8 12 16 20 24 28 32 36 40 44 48 4 | 5 10 15 20 25 30 35 40 45 50 55 60 5 | 6 12 18 24 30 36 42 48 54 60 66 72 6 | 7 14 21 28 35 42 49 56 63 70 77 84 7 | 8 16 24 32 40 48 56 64 72 80 88 96 8 | 9 18 27 36 45 54 63 72 81 90 99 108 9 | 10 20 30 40 50 60 70 80 90 100 110 120 10 | 11 22 33 44 55 66 77 88 99 110 121 132 11 | 12 24 36 48 60 72 84 96 108 120 132 144 12 | 13 26 39 52 65 78 91 104 117 130 143 156
Last edited by AcousticBruce; 12-24-2010 at 03:24 AM.
- 12-24-2010, 03:43 AM #11
Member
- Join Date
- Nov 2010
- Posts
- 22
- Rep Power
- 0
Thanks a lot AcousticBruce
I appreciate the time and effort. Have a great day!
- 12-24-2010, 05:56 AM #12
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 12-24-2010, 05:58 AM #13
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 12-24-2010, 06:00 AM #14
Member
- Join Date
- Nov 2010
- Posts
- 20
- Rep Power
- 0
a
I think the positions of the numbers (left side) are not in correct positions. :confused:
- 12-24-2010, 06:02 AM #15
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 12-24-2010, 06:11 AM #16
Member
- Join Date
- Nov 2010
- Posts
- 22
- Rep Power
- 0
Eranga,
I think the answer by AcousticBruce comes close enough. Thank you both for your help.
- 12-24-2010, 06:15 AM #17
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You are welcome. If you satisfied with the solution given by AcousticBruce, then please mark the thread solved. :)
- 12-24-2010, 06:58 AM #18
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 3
Just for the hell of it, I added a couple lines and changed some numbers (in red). Dont worry I have a blast doing this stuff.
The new outputJava Code:public class TestApps { public static void main(String[] args) { int[][] tTable = new int[COLOR="Red"][15][14][/COLOR]; for (int row = 0; row < [COLOR="Red"]14[/COLOR]; row++) { for (int col = 0; col < 13; col++) { if (col == 1) { //This creates the device of "|". if (row != 0 [COLOR="Red"]&& row != 1[/COLOR]) { // if (row > 1) would have worked just as well :D System.out.print("| "); // I added 2 spaces after "|" to make it look better. } else { System.out.print(" "); // I added 2 more spaces to " " as I did above. Otherwise it skews. } } [COLOR="Red"] if (row == 0) { //Added this for the top row line 1-12. tTable[row][col] = col; }[/COLOR] tTable[COLOR="Red"][row+1[/COLOR]][col] = [COLOR="Red"](row) [/COLOR]* (col ); if (row*col == 0) { // Added this for the left column line 1-12. tTable[row+1][0] = row; } if (tTable[row][col] == 0) { //This puts the space in the top left corner. System.out.print(" "); //added 3 " " spaces here. } else { System.out.print(tTable[row][col] + "\t"); } } System.out.println(); } } }
Java Code:1 2 3 4 5 6 7 8 9 10 11 12 1 | 1 2 3 4 5 6 7 8 9 10 11 12 2 | 2 4 6 8 10 12 14 16 18 20 22 24 3 | 3 6 9 12 15 18 21 24 27 30 33 36 4 | 4 8 12 16 20 24 28 32 36 40 44 48 5 | 5 10 15 20 25 30 35 40 45 50 55 60 6 | 6 12 18 24 30 36 42 48 54 60 66 72 7 | 7 14 21 28 35 42 49 56 63 70 77 84 8 | 8 16 24 32 40 48 56 64 72 80 88 96 9 | 9 18 27 36 45 54 63 72 81 90 99 108 10 | 10 20 30 40 50 60 70 80 90 100 110 120 11 | 11 22 33 44 55 66 77 88 99 110 121 132 12 | 12 24 36 48 60 72 84 96 108 120 132 144 Process completed.Last edited by AcousticBruce; 12-24-2010 at 07:14 AM.
Similar Threads
-
need help making a multiplication table
By KAM0002 in forum New To JavaReplies: 17Last Post: 12-07-2010, 02:44 AM -
need help with this: multiplication table
By MsIceCold in forum New To JavaReplies: 7Last Post: 07-13-2010, 01:31 PM -
Multiplication Table
By SwEeTAcTioN in forum New To JavaReplies: 4Last Post: 02-24-2010, 04:11 AM -
Multiplication table
By rjones215 in forum New To JavaReplies: 3Last Post: 10-19-2009, 04:34 PM -
Help with multiplication table
By Albert in forum New To JavaReplies: 1Last Post: 07-10-2007, 04:44 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks