CheeseCracker
CheeseCracker

Reputation: 35

Triangular multiplication table in java

I'm trying to create a program in java that would create a 10 (row) by 15 (column) triangular multiplication table for an assignment. In the assignment we have to use constants IMAX= 10 and JMAX=15. I have attached a picture below of what the table should look like. I've gotten the jist of the table but am having trouble trying to stop the math and row 10 and also adding a border in the column numbers. Help would be appreciated.
Also to do this problem, we are permitted to use if/else statements, for loop and/or while loop

Multiplication table link

public class Question2 {
    public static void main(String[] args) {
            // TODO Auto-generated method stub

        final int IMAX= 15, JMAX=10;    

        for (int i = 1; i <= JMAX; i++) 
        {
            System.out.print( i + "  ");
        }

        System.out.println();
        System.out.print("--------------------------------------------------------" );
        System.out.println();

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

Upvotes: 1

Views: 531

Answers (4)

Suparno Saha
Suparno Saha

Reputation: 115

Here is multiplication table in java

import java.util.*;
public class table
{
    public static void main()
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a number");
        int a = sc.nextInt();
        for (int i=1; i <=12; i++)
        System.out.println(a+ "    x    " +i + "   =   " +a*i);
    }
}

You can change the variable names whatever you want

Upvotes: 1

Rakesh Malik
Rakesh Malik

Reputation: 689

This should do:

public class HelloWorld {

    public static void main(String[] args) {
        final int IMAX= 15, JMAX=10;

        System.out.print("   |");
        for (int i = 1; i <= JMAX; i++) {
            System.out.printf("%-4d", i);
        }
        System.out.println();
        System.out.print("--------------------------------------------------------" );
        System.out.println();

        for (int i = 1; i <= IMAX; i++) {
            System.out.printf("%-3d|", i);
            for (int j = 1; j <= i && j <=JMAX; j++) {
                System.out.printf("%-4d", i*j);
            }
            System.out.println();
        }

    } 

}

Upvotes: 0

LppEdd
LppEdd

Reputation: 21124

You could do it in millions different ways!

public class Test
{
   private static final int ROWS = 15;
   private static final int COLS = 10;

   public static final void main(final String... args) {
      for (int i = 0; i <= ROWS; i++) {
         if (i < 1) {
            System.out.printf("%5s", " | ");

            for (int k = 1; k <= COLS; k++) {
               System.out.printf("%-4d", k);
            }

            System.out.println();
            System.out.println("____________________________________________");
            continue;
         }

         System.out.printf("%5s", i + " | ");

         for (int j = 1; j <= i && j <= COLS; j++) {
            System.out.printf("%-4d", i * j);
         }

         System.out.println();
      }
   }
}

Upvotes: 0

sylencehero
sylencehero

Reputation: 56

   public static void main(String[] args) {
        final int IMAX = 15, JMAX = 10;
        System.out.print("   |");
        for (int i = 1; i <= JMAX; i++) {
            System.out.print(i + "  ");
        }
        System.out.println();
        System.out.print("--------------------------------------------------------");
        System.out.println();
        for (int i = 1; i <= IMAX; i++) {
            if (i < 10) {
                System.out.print(i + "  |");
            } else {
                System.out.print(i + " |");
            }
            for (int j = 1; j <= i; j++) {
                System.out.print(i * j + " ");
                if (j == JMAX) {
                    break;
                }
            }
            System.out.println();
        }
    }

Upvotes: 1

Related Questions