10

How can a multiplication table be displayed using only nested loops and System.o...

 2 years ago
source link: https://www.codesd.com/item/how-can-a-multiplication-table-be-displayed-using-only-nested-loops-and-system-out-println-in-java.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How can a multiplication table be displayed using only nested loops and System.out.println in Java?

advertisements
public static void main (String [] args)
{
    int q, r, s, t, u, v, w, x, y, z;

    for (q=1; q<=10; q++)
    {
        System.out.print("\t" + q);
    }
    System.out.println();
    for (r=2; r<=20; r += 2)
    {
        System.out.print("\t" + r);
    }
    System.out.println();
    for (s=3; s<=30; s += 3)
    {
        System.out.print("\t" + s);
    }
    System.out.println();
    for (t=4; t<=40; t += 4)
    {
        System.out.print("\t" + t);
    }
    System.out.println();
    for (u=5; u<=50; u += 5)
    {
        System.out.print("\t" + u);
    }
    System.out.println();
    for (v=6; v<=60; v += 6)
    {
        System.out.print("\t" + v);
    }
    System.out.println();
    for (w=7; w<=70; w += 7)
    {
        System.out.print("\t" + w);
    }
    System.out.println();
    for (x=8; x<=80; x += 8)
    {
        System.out.print("\t" + x);
    }
    System.out.println();
    for (y=9; y<=90; y += 9)
    {
        System.out.print("\t" + y);
    }
    System.out.println();
    for (z=10; z<=100; z += 10)
    {
        System.out.print("\t" + z);
    }

}

Despite how ridiculous this program looks, it displays a multiplication table in the desired format. Being a noob (as you can see) and trying to learn these nested loops has been very confusing, especially when the tutorial says that this multiplication table, in the same exact format, can be written using just nested for loops and System.out.println. The tutorial is not the least bit helpful and gives a rather simple use of nested for loops and right now I cannot see how it's applicable in simplifying this program....but it says it can be done so it can.


This is something that you will learn in any beginners book:

for (int i=1;i<=10;i++){
  for (int j=1;j<=10;j++)
     System.out.print("\t"+i*j);
  System.out.println();
}


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK