6 Replies - 4767 Views - Last Post: 30 September 2012 - 06:52 AM Rate Topic: -----

#1 Kilanz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-September 12

The Sum Of The Columns In Multiplication Table

Posted 30 September 2012 - 01:58 AM

How Can I Add The Numbers In The Columns Of The Multiplication Table ?

For Example

1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16
10 20 30 40 = Total

import java.awt.*;
import java.util.*;
import java.io.*;

public class MultiplicationTable{
    public static void main(String[] args) throws IOException
    {
    	
        int[][] multtable = new int[11][11];

		System.out.print( "Enter your number: " );
		java.io.BufferedReader maankilanz = new java.io.BufferedReader( new java.io.InputStreamReader( System.in ) );
		
		String input = maankilanz.readLine();
		int n = Integer.parseInt( input );

        System.out.print("*");
		for (int a = 1; a <= n; a++) {
		System.out.print("\t" + a);
}
		System.out.println(" ");
		for (int i = 0; i <= n; i++) {
		
		for (int j = 0; j <= n; j++) {
		
		multtable[i][j] = i * j;
	}
}
		System.out.print("\t");
		System.out.print("-----------------------------------------------------------------------------");
		System.out.println("");

		for (int i = 1; i <= n; i++) {

		System.out.print(i + "|");

		for (int j = 1; j <= n; j++)

	{
		System.out.print("\t" + multtable[i][j]);
	}
		System.out.println("");
		System.out.println("");
}
    }
}


This post has been edited by baavgai: 30 September 2012 - 03:03 AM
Reason for edit:: moved and tagged, java ain't python


Is This A Good Question/Topic? 0
  • +

Replies To: The Sum Of The Columns In Multiplication Table

#2 Larry71  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 43
  • Joined: 05-June 12

Re: The Sum Of The Columns In Multiplication Table

Posted 30 September 2012 - 03:21 AM

I don't know why,but your post was split in two and my answer that I gave was disappeared.

As I told before,the sum of n numbers is n*(n+1)/2.For example the sum from 1 to 10 is 10*11/2=55.
So the 10,20,30,40 you are writing in the example,are rows*(rows+1)/2 in a loop where you use the number of the cols and multiply each time with.

If not wrong.
Was This Post Helpful? 0
  • +
  • -

#3 Kilanz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-September 12

Re: The Sum Of The Columns In Multiplication Table

Posted 30 September 2012 - 03:52 AM

I Dont Know How To Debug It I Tried Many Times But Still So Many Errors. I Can't Make The Code Add The Numbers Inside The Cols.
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: The Sum Of The Columns In Multiplication Table

Posted 30 September 2012 - 04:16 AM

If there are compiler errors you want help with, post them, copied and pasted just as they appear at your end. If there are logic errors that cause the program's results to be incorrect, describe the current results and the desired results, using actual or sample runs as available.

This is what I get when I run your program:
Enter your number: 5
*	1	2	3	4	5 
	-----------------------------------------------------------------------------
1|	1	2	3	4	5

2|	2	4	6	8	10

3|	3	6	9	12	15

4|	4	8	12	16	20

5|	5	10	15	20	25

What should it be?

This post has been edited by GregBrannon: 30 September 2012 - 04:18 AM

Was This Post Helpful? 0
  • +
  • -

#5 Kilanz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-September 12

Re: The Sum Of The Columns In Multiplication Table

Posted 30 September 2012 - 05:25 AM

The Code Should Let the User Input How Many Rows And Columns. And When The Multiplication Table Isa Done. Then The Addition Will Take Place Like This:


Enter your number: 5
* 1 2 3 4 5
-----------------------------------------------------------------------------
1| 1 2 3 4 5

2| 2 4 6 8 10

3| 3 6 9 12 15

4| 4 8 12 16 20

5| 5 10 15 20 25
15 30 45 60 75 This Row Is the Total Of the Following Columns

I Wish You'll Get It. Hehe
Was This Post Helpful? 0
  • +
  • -

#6 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: The Sum Of The Columns In Multiplication Table

Posted 30 September 2012 - 05:45 AM

Hehe, and I was hoping you'd get it.

The sum of each column could be calculated either by formulas (probably a pain) or by summing the values in each row of each column in nested for loops. You might use a new array, sumOfColumns[], that has as many members as their are columns, then use nested for loops to calculate:
for ( the number of columns )
{
    for ( the number of rows )
    {
        sumOfColumns[column] += multTable[row][column];
    }
}

Instead of creating an additional set of nested for loops to make this calculation, a goal should be to include it in the loops you already have.
Was This Post Helpful? 0
  • +
  • -

#7 Kilanz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-September 12

Re: The Sum Of The Columns In Multiplication Table

Posted 30 September 2012 - 06:52 AM

View PostGregBrannon, on 30 September 2012 - 05:45 AM, said:

Hehe, and I was hoping you'd get it.

The sum of each column could be calculated either by formulas (probably a pain) or by summing the values in each row of each column in nested for loops. You might use a new array, sumOfColumns[], that has as many members as their are columns, then use nested for loops to calculate:
for ( the number of columns )
{
    for ( the number of rows )
    {
        sumOfColumns[column] += multTable[row][column];
    }
}

Instead of creating an additional set of nested for loops to make this calculation, a goal should be to include it in the loops you already have.


How Does Thje Code Go Sir ? I Got It But Where Should I Put The Code Snippet You Made. Sorry I'm a Noob Programmer :((
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1