Let’s write a simple Java program to generate a multiplication table. It’s fairly a simple program.
I am going to write this as a dynamic multiplication table generator, so user can enter which table they want to print and up to which limit.
Here is the complete Java Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
/** * * @author agur */ import java.util.Scanner; public class GenMulti { public static void main(String[] args) { //define the variables int table_no, upto, i; Scanner in = new Scanner(System.in); //get the Table no value System.out.print("Table no:"); table_no = in.nextInt(); //get the upto limit System.out.print("Upto:"); upto = in.nextInt(); //loop through the upto limit //print the table for (i=1; i<=upto; ++i){ System.out.println(table_no+"*"+i+"="+table_no*i); } } } |
Just go through the inline comments in the above program to understand it.
Sample Output of the above program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
run: Table no:12 Upto:12 12*1=12 12*2=24 12*3=36 12*4=48 12*5=60 12*6=72 12*7=84 12*8=96 12*9=108 12*10=120 12*11=132 12*12=144 BUILD SUCCESSFUL (total time: 5 seconds) |
Enjoy Coding & Have fun.