# Thread: Multiplication table in Java

1. Registered User
Join Date
Nov 2005
Location
London
Posts
23

## Multiplication table in Java

HI all ok here I need a quick help I am trying to Design and develop an application that displays a multiplication table based on the number to be multiplied, and how many lines are to be displayed.

I will be expected to create a new project and build a class from scratch that will implement a solution.

The user might enter 9 as the table to be shown and 12 as the number of lines. In that case, the output would look like this:

1 x 9 = 9
2 x 9 = 18 and so on up to 12 x 9 any help will be great thank you bye.

2. Sounds like you've got a pretty good idea of what you need to do. I suggest you give it a shot first and see what you can come up with.

If you're looking for help getting started, I find that oftentimes the first step in writing a program is to turn off the computer. After that, sit down with a pencil and some paper and write out what elements you know you're gonna need, and consider how you can make them interact. Turn the computer back on, start coding what you've written down, and see where that takes you -- often you'll find by that time that you've got a firmer grasp of exactly what you're up against, and the rest should be, as they say, easy as pie (or cake...or cheese...maybe pizza? better stop before I make myself hungry).

Anywho, give it a whirl, and if you get stuck, drop back in here for a nudge or two in the (hopefully) right direction!

3. Registered User
Join Date
Nov 2005
Location
London
Posts
23
Cytael Thx after you told me to start something here what I did OK I created 2 class's and I called them The Factorial Calculator and The Displays a Factorial link them up and here how I started first code for The Displays a Factorial class is:

public class FactorialDisplay
{

/**
* Constructor for objects of class FactorialDisplay
*/
public FactorialDisplay()
{
calculator = new FactorialCalculator();
}

/**
* Displays the factorial of a positive number less than 10 calculated by three different methods.
* Should print three identical lines
* @param the integer whose factorial is to be calculated
*/
public void display(int n)
{
if(n > 0 && n < 10)
{
System.out.println("The factorial of " + n + " using a for loop is: " + calculator.factorialByForLoop(n));
System.out.println("The factorial of " + n + " using a while loop is: " + calculator.factorialByWhileLoop(n));
System.out.println("The factorial of " + n + " using a do..while loop is: " + calculator.factorialByDoWhileLoop(n));
}
else
{
System.out.println("" + n + " is out of scope");
}
}
}

that was for The Displays a Factorial next one was The Factorial Calculator thats where I am stock now don't know what to do next

public class FactorialCalculator
{

}

I need a code there to calculate the factorial of 1x9 up to 12x9 and also to print a print out.

4. Registered User
Join Date
Feb 2005
Posts
34
Code:
class Test {
public static void main(String[] args) {
int n = 2, upper = 10;
String output = "";

for (int i = 1; i <= upper; i++) {
output += String.valueOf(i * n) + "\n";
}
System.out.println(output);

output = "";
int i = 1;
while (i <= upper) {
output += String.valueOf(i * n);
}
System.out.println(output);

output = "";
i = 1;
do {
output += String.valueOf(i * n);
i++;
} while (i <= upper);
System.out.println(output);
}
}
By print, do you mean print on the command prompt? Use System.out.println(). If you mean actually print on paper, use java.io.PrintWriter.

BTW, you probably need to change your method definitions. You need to have 2 arguments for each: the number to be multiplied and the upper limit of the multiplication.

5. Registered User
Join Date
Nov 2005
Location
London
Posts
23
Thx for the code pyroclasm ok after I install it on my program I still can't do 1 x 9 it just don't let me do it I don't know why

About the print out yes I mean using system.out.println so I enter 9 it should give me all 9 x 9s up to 9 x 12 = ?

6. Well eventhough in ur messageu clearly posted that its "your task", I still am willing to write a short program for you..take a look below and let me know if thats what u wanted...

PHP Code:
import java.io.IOException;

/**
* Created by IntelliJ IDEA.
* User: Khalid Ali
* Date: Nov 26, 2005
* Time: 10:52:00 AM
*/
public class TableGenerator {

public
TableGenerator() {

String tableNum ""tableLines "";

while (
true) {//always look for new input unless user exits the program

}
}

/**
*
* @param keyIn
*/

String message "Please select a number from the options below and hit Enter key";

message += "\n1:  Create a new table";

message += "\n2:  Quit";

try {

if (
cmd.endsWith("2")) {//exit program if user enters 2

System.exit(0);
}

getTableData(keyIn);   //continue on wit table generation

} catch (IOException e) {

e.printStackTrace();
}

}

public

String tableNumStr      =   "";

String tableLinesStr    =   "";

int tableNum            =   0;

int tableLines          =   0;

try {
//get user input for table to be shown

System.out.println("Enter number for which table to be created and hit Enter key:");

System.out.println("Enter number of lines 2 be displayed for the table and hit Enter key:");

tableNum    Integer.parseInt(tableNumStr);

tableLines  Integer.parseInt(tableLinesStr);

generateTable(tableNumtableLines);
} catch (
NumberFormatException e) {

e.printStackTrace();
} catch (
IOException e) {

e.printStackTrace();
}
}

private
void generateTable(int tableNumint tableLines) {

System.out.println("\n ***** Table ******\n");
for(
int n=0;n<tableLines;n++){

int multipliedBy;

multipliedBy n+1;

System.out.println(multipliedBy +" X "tableNum " = "+(multipliedBy tableNum));
}
}

public static
void main(String[] args) {
new
TableGenerator(); //constructor to initiate the whole process

}

Edit:
Make sure you remove autognerated comments at the top of the class displaying my name and date etc by Intellij before using this class..:-)
It will not reflect too good if u claimed it to be written by u with those comments at the top...hehehe
Last edited by Khalid Ali; 11-26-2005 at 11:44 PM.

7. Registered User
Join Date
Nov 2005
Location
London
Posts
23
Thx for the program Khalid Ali but thats not what I want

OK what I want is for a program to print.out a table of 1 x 9 = 9 up to 12 x 9 = 108 just that and the print.out should look like this

1 x 9 = 9
2 x 9 = 18
3 x 9 =
4 x 9 =
5 x 9 =
6 x 9 =
7 x 9 =
8 x 9 =
9 x 9 =
10 x 9 =
11 x 9 =
12 x 9 = 108

thats all so for example when I enter 9 it should give me that table.

BTW this is just a practice on java for me its not a Homework or something but it would be great if I know how I can do this

8. You shouldn't need anything much more complicated than this
PHP Code:
public PrintTable(int baseint rows)
{
for(
int i=1i<=rowsi++)
{

System.out.println(" x " base " = " (i*base));
}

I'll leave the exact method used for passing in the parameters up to you.

9. by the way, what you're asking for in this table is very different from the factorials you alluded to in your second post.

a factorial is the product of a positive integer times all positive integers less than it, such that 9 factorial = 9! = 9x8x7x6x5x4x3x2x1 = 362880

one way to calculate a factorial using a for loop would be:
PHP Code:
int factorial 9//hardcoding 9 for demo purposes
int product 1;
for(
int i=factoriali>0i--)

product *= i//does that work in java? if not, use product = product*i
System.out.println(factorial "! = " product);

10. Originally Posted by Nez2003
Thx for the program Khalid Ali but thats not what I want

OK what I want is for a program to print.out a table of 1 x 9 = 9 up to 12 x 9 = 108 just that and the print.out should look like this

1 x 9 = 9
2 x 9 = 18
3 x 9 =
4 x 9 =
5 x 9 =
6 x 9 =
7 x 9 =
8 x 9 =
9 x 9 =
10 x 9 =
11 x 9 =
12 x 9 = 108

thats all so ....
I wonder if you even tried to run the example program I posted above...take a look at the example out put above which u posted and compare it with the out put of my program below...

Code:
Please select a number from the options below and hit Enter key
1:  Create a new table
2:  Quit
1
Enter number for which table to be created and hit Enter key:
9
Enter number of lines 2 be displayed for the table and hit Enter key:
12

***** Table ******

1 X 9 = 9
2 X 9 = 18
3 X 9 = 27
4 X 9 = 36
5 X 9 = 45
6 X 9 = 54
7 X 9 = 63
8 X 9 = 72
9 X 9 = 81
10 X 9 = 90
11 X 9 = 99
12 X 9 = 108

Please select a number from the options below and hit Enter key
1:  Create a new table
2:  Quit
2

Process finished with exit code 0
And this is not complex by any means, logic is very simple only a bit modular...

11. Registered User
Join Date
Nov 2005
Location
London
Posts
23
Khalid Ali I tried your program and I saw what it did user enters a number and it calculates it for them but thats not what I am after I know your program dose 1 x 9 = 9 and so on but the program I am after is I create a class then click on it create lets say calculate 1 x 9 then I enter 9 and I get a print.out that show me all 9x 9's from 1 up 12

12. Registered User
Join Date
Feb 2005
Posts
34
I don't completely understand what you just posted. Do you want to separate the logic from the view (model-view) like a GUI and have user input from that? Then you would use the same algorithm Khalid and I posted but have the GUI code in another class.

If you want to use just an instance of a class, then it would look something like this:
Code:
class Test {
int number;

public Test(int n) {
this.number = n;
}

private void doPrint() {
int product;
for (int i = 1; i <= 12; i++) {
product = this.number * i;
System.out.println(this.number + " x " + i + " = " + product);
}
}

public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
Test test = new Test(n);
test.doPrint();
}
}

13. Originally Posted by Nez2003
..... I am after is I create a class then click on it create lets say calculate 1 x 9 then I enter 9 and I get a print.out that show me all 9x 9's from 1 up 12
What...????

you have say it in plain english so that its understandable for poor software developers as welll...:-)

Seems like you want a GUI stand alone app where you have a "button" which upon a click show 2 fields where user can enter the values and then you show the table in that GUI somwhere? is that what you are after?

14. Registered User
Join Date
Nov 2005
Location
London
Posts
23
OPs sorry Khalid Ali you right I was very tired and I came from work so just posted that without thinking

OK any away finally after working on the java I get what I wanted to get here is the code for it

public class Multiplication
{
// a multiplication table

public void show ( int facter)
{
int Multiplication;

for ( int i = 1; i < 13 ; i++)
{
Multiplication = i * facter; // "i" is been muliplyed by facter which was entered by the user.
System.out.println("" + i + " x " + facter + " = " + Multiplication );
}

}

}

This is was what I was after any away thank you guys so much for the all help thank you.

15. Registered User
Join Date
Nov 2005
Location
London
Posts
23
Deam another mistake for get about ''is'' I don't know whats wrong with me these days this stupid java programing makes me feel ill