Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: need help java multiplication table

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    53
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default need help java multiplication table

    the output should be
    rows 5
    column 5
    1 2 3 4 5 =120
    2 4 6 8 10 =3840
    3 6 9 12 15 =29160
    4 8 12 16 20 =122880
    5 10 15 20 25 =375000
    =120 =3840 =29160 =122880 =375000

    it means getting the product of each row and each column
    import javax.swing.*;
    public class activity2
    {
    public static void main(String []args)
    {
    int r=Integer.parseInt(JOptionPane.showInputDialog(null,"rows"));
     
    int c=Integer.parseInt(JOptionPane.showInputDialog(null,"cols"));
     
    int table[][]= new int [r][c];
    for(int i=0;i<=table.length-1;i++){
    for(int j=0; j<=table[0].length-1;j++){
    table [i][j]= (i+1)*(j+1);
    if(table[i][j]<0)
    System.out.print(table[i][j]+"\t");
    else
    if(table[i][j] >10 && table [i][j] <100)
    System.out.print(table[i][j]+"\t");
    else
    System.out.print(table[i][j]+"\t");
    }
    System.out.println("");
    }
     
     
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: need help java multiplication table

    Can you post the current output and describe what is wrong with it?

    Please edit your code and add proper formatting. Statements should not all start in the first column. There should be indentations of 3-4 spaces for each level of nested logic within each pair of {}s.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Oct 2012
    Posts
    53
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help java multiplication table

    output is

    1 2 3 4 5
    2 4 6 8 10
    3 6 9 12 15
    4 8 12 16 20
    5 10 15 20 25

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: need help java multiplication table

    The second part of my post asked:
    Can you explain what is wrong with the output?


    Please edit your code and add proper formatting. Statements should not all start in the first column. There should be indentations of 3-4 spaces for each level of nested logic within each pair of {}s.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Oct 2012
    Posts
    53
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help java multiplication table

    my code is fine but i do not know how to get the product of each row and each column...like what i posted to my first post..

    sorry about the indentions..

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: need help java multiplication table

    Start with the numbers on a row. Define a variable to hold the product. As each number for that row is created, include its value in the product for that row. After the last number for the row, print it.

    When that code works, work on the logic for the columns. Best to do one at a time.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Oct 2012
    Posts
    53
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help java multiplication table

    can you teach me how to do it??

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: need help java multiplication table

    Do the code for the rows first. See post#6 for ideas.

    When that works, then work on the columns.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Oct 2012
    Posts
    53
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help java multiplication table

    i have really no idea what will i code to hold the values of row 1

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: need help java multiplication table

    Outside the loop, define an int variable with initial value of 0
    inside the loop add each number that is generated and printed to the variable
    after the loop print the value of the variable at the end of the row.

    You need to fix the formatting of your code if you want anyone to look at it.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Multiplication program Loop
    By PhilThomspon in forum Loops & Control Statements
    Replies: 7
    Last Post: May 6th, 2012, 03:29 PM
  2. Multiplication program
    By PhilThomspon in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 6th, 2012, 01:54 PM
  3. Having problem in matrix multiplication....
    By sidhant in forum Java Theory & Questions
    Replies: 5
    Last Post: March 17th, 2011, 01:41 PM
  4. Recursive multiplication and Karatsuba
    By jsp01 in forum Algorithms & Recursion
    Replies: 0
    Last Post: March 14th, 2011, 02:04 AM
  5. Multiplication code
    By bomboy in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 5th, 2011, 02:25 AM