Monday, September 13, 2010

Sample Code for Multiplication Table Generation

// by Dale D. Miguel
import java.util.*;
public class MTable {
public static void main(String[] args) {
Scanner kbd = new Scanner(System.in);
int start, increment, rows, columns;
System.out.println("Multiplication Table Generation Program.");
do{
System.out.print("Start Value: ");
start = Integer.parseInt(kbd.nextLine());
System.out.print("Increment: ");
increment = Integer.parseInt(kbd.nextLine());
rows = readPositiveInteger("Enter the number of rows");
columns = readPositiveInteger("Enter the number of columns");
if (columns > 10){
System.out.println("The screen cannot accomodate more than 10 columns.");
System.out.println("It is suggested that you generate more than one multiplication tables.");
System.out.println("Please use at most 10 columns for each table.");
}
} while (columns > 10);
showMultiplicationTable(start, increment, rows, columns);
System.exit(0);
}

public static void showMultiplicationTable(int start, int step, int rows, int columns){
int ctrCol=0, ctrRow=0;
System.out.println(" M U L T I P L I C A T I O N T A B L E");
int colVal = start, rowVal=start;

//generate and print the first row
for (ctrCol= start; ctrCol< start+step*columns; ctrCol=ctrCol+step){
System.out.printf("%6d", ctrCol);
}
System.out.println();
//generate and print second row, third row, fourth row and so on.
for (ctrRow =start+step; ctrRow rowVal = ctrRow;
for ( ctrCol= start ; ctrCol< start + step*columns; ctrCol = ctrCol+step){
if (ctrCol == start )
colVal = 1;
else
colVal = ctrCol;
System.out.printf("%6d", rowVal*colVal);
}
System.out.println();
}
return;
} //end of showMultiplicationTable method

public static int readPositiveInteger(String prompt){
Scanner kbd = new Scanner(System.in);
int result=0;
do {
System.out.print(prompt + ": ");
result = Integer.parseInt(kbd.nextLine());
if (result <1)
System.out.println("Negative Value not allowed! Please try again.");
}while (result < 1);

return result;
}

}// end of class

4 comments:

  1. Sir I have a question all about in multiplication table because what i did is different i dont know if it is correct here sir.

    public class Table{
    public static void main(String []args){

    System.out.println("== Multiplication Table ==");

    System.out.print(" ");

    for(int x=1;x<=10;x++){
    System.out.printf("%6d",x);
    }
    System.out.println();

    for(int x=1;x<=10;x++){
    System.out.print(x);

    for(int y=1;y<=10;y++){
    if(x>=10&&y==1){
    System.out.printf("%5d",x*y);
    }
    else
    System.out.printf("%6d",x*y);
    }
    System.out.println();
    }
    }
    }

    ReplyDelete
  2. To Mr. Desiderio:
    Your program for the generation of a multiplication program is correct. However, it is better if you make it flexible so that the user can use it to generate a multiplication table depending on his/her desired parameter(e.g. starting number, number of rows...)

    ReplyDelete
  3. Sir I can't understand this...it is on your blog...

    for (ctrRow =start+step; ctrRow rowVal = ctrRow;

    What is the correct format Sir I can't fix it???...

    ReplyDelete