Results 1 to 18 of 18
  1. #1
    BillyB is offline Member
    Join Date
    Nov 2010
    Posts
    22
    Rep Power
    0

    Default 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 class
    Last edited by Eranga; 12-24-2010 at 02:09 AM. Reason: code tags added

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Java Code:
    System.out.print((row - 1) + " | ");
    where to add that?

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    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. :)

  4. #4
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    5

    Default

    Hey Billy.


    Can you be a little more clear on what you are trying to accomplish?

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  6. #6
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    5

    Default

    Quote Originally Posted by Eranga View Post
    With the description at the top, he's clear with what he wants to do, isn't it. ;)

    For you maybe.
    I would like the zero space and | to be a blank space in this line
    that doesn't make sense to me.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  8. #8
    BillyB is offline Member
    Join Date
    Nov 2010
    Posts
    22
    Rep Power
    0

    Default 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.

  9. #9
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    5

    Default

    ......... Edit
    Last edited by AcousticBruce; 12-24-2010 at 03:58 AM.

  10. #10
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    5

    Default

    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 04:24 AM.

  11. #11
    BillyB is offline Member
    Join Date
    Nov 2010
    Posts
    22
    Rep Power
    0

    Default Thanks a lot AcousticBruce

    I appreciate the time and effort. Have a great day!

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by AcousticBruce View Post
    If you have a different font for your output and it is skewed. Just delete the extra " " spaces in the code.
    With spaces, but not with tabs. But again align is difficult with different number of digits.

  14. #14
    temyong is offline Member
    Join Date
    Nov 2010
    Posts
    20
    Rep Power
    0

    Default a

    I think the positions of the numbers (left side) are not in correct positions. :confused:

  15. #15
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by temyong View Post
    I think the positions of the numbers (left side) are not in correct positions. :confused:
    You mean digit sequences are not matched. As I said in my previous post, formatting is bit of difficult in such case. AcousticBruce done it with use spaces nicely, and I hope he may have confusion with adding those spaces. :)

  16. #16
    BillyB is offline Member
    Join Date
    Nov 2010
    Posts
    22
    Rep Power
    0

    Default

    Eranga,

    I think the answer by AcousticBruce comes close enough. Thank you both for your help.

  17. #17
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    You are welcome. If you satisfied with the solution given by AcousticBruce, then please mark the thread solved. :)

  18. #18
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    5

    Default

    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.

    Java 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();
            }
        }
    }
    The new output
    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 08:14 AM.

Similar Threads

  1. need help making a multiplication table
    By KAM0002 in forum New To Java
    Replies: 17
    Last Post: 12-07-2010, 03:44 AM
  2. need help with this: multiplication table
    By MsIceCold in forum New To Java
    Replies: 7
    Last Post: 07-13-2010, 02:31 PM
  3. Multiplication Table
    By SwEeTAcTioN in forum New To Java
    Replies: 4
    Last Post: 02-24-2010, 05:11 AM
  4. Multiplication table
    By rjones215 in forum New To Java
    Replies: 3
    Last Post: 10-19-2009, 05:34 PM
  5. Help with multiplication table
    By Albert in forum New To Java
    Replies: 1
    Last Post: 07-10-2007, 05:44 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •