Hello everyone,
I need to make a program for Java that has the user enter in 2 numbers,1-9, then prints a top row and column with those numbers in it, then prints a multiplication table for those numbers. I need to use a for loop or for loops.

Ex.

2 3 4 5 6

2 4 6 8 10 12

3 6 9 12 15 18

4 8 12 16 20 24

5 10 15 20 25 30

6 12 18 24 30 36


the top row is messed up but u get the idea
Thanks in advance

Recommended Answers

All 9 Replies

Show us what you have so far so we can help you.

So start writing code and let us know if you get stuck on anything specific. Best of luck :)

Mike

i only have the scanner console set up and the 2 variables for the console set up.

import java.util.*;

public class Tables {
public static void main(String[] args){
int firstNumberForTable;
int secondNumberForTable;

Scanner console = new Scanner(System.in);

System.out.println("Enter the first # for the Multiplication Table: " + "\n");
firstNumberForTable = console.nextInt();
System.out.println("Enter the second # for the Multiplication Table: " + "\n");
secondNumberForTable = console.nextInt();
System.out.println("Multiplication Table for " + firstNumberForTable + " to " + secondNumberForTable);
}
}

i really need an example because im unsure of what to put in the for loop(s) and where the variables fit in to make it print out the way i need

One, use code tags.

[code]

// paste code here

[/code]


Two, you need to use a nested loop:

int i;
int j;
int someNumber;


for(i = 1; i < ??; i++)
{
    for(j = 1; j < ??; j++)
    {
        someNumber = ?? * ??;
        // display someNumber
    }
}

Fill in the ??. You also need some line returns in there that I left out.

i assume if they enter 9 then 8, its the 2 through 9 times table maybe carried through to 2 * 2 , 2 * 3, 2 * 4, etc 2 * 8 through 9 * 8?

maybe one loop can build a string that represents a row. then print line that row, the outer loop loops through the rows. so first row you build 2 4 6 8 10 12 14 16, in a string with concatenation then print. Make sure you set your string to = "" as you loop through the rows in the outer loop to reset it.

Mike

thank u bro, i got it to print out in order and in the correct format, only thing is it prints a full multiplucation table from 1-9 and i need it to print out a table between the numbers that are entered

I assumed the two numbers were the number of rows and the number of columns. if the two numbers represent start row and end row, then columns has to be assumed at some value. I.e. if they enter 3 and 8, your row loop is for(int i=3; i<=8; i++) perhaps if you go all the way to 8. your inner loop to build the row and its columns has to be to some default value, i.e .maybe you always make a row 12 places or 8 places. i.e. for 6 columns and the first row is 3,

3 6 9 12 15 18 where your inner loop or columns to build the string defaults to 6.

So essentially you just need to have your row loop go between the two values entered, if this is the implementation you want.

thanks again, i got it to work...solved

I also have a similar task if anyone can help.
Goal is for user to input 2 numbers then get a multiplication chart with a row and column.
Example: user enters 3 and 5, then should print

...3    4   5       (disregard the dots)
3  9    12  15  
4  12   16  20  
5  15   20  25

My program will not correctly put the chart next to the column. any suggestions?

import java.util.*;

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



Scanner scan = new Scanner(System.in);

    System.out.println();
    System.out.println("Multiplication Table");
    System.out.println();
    System.out.println("Enter Values");


    int a = scan.nextInt();
    int b = scan.nextInt();

     if(a>b){
       int c = a; 
       int d = b;
       a = d;
       b = c; 
    }


      for(int q=a;q<=b;q++){

        System.out.print("\t"+q+"\t");

      }
      for(int q=a;q<=b;q++){

        System.out.print("\n"+q);
      }
      System.out.println();

    for(int i = a; i<=b; i++){
      for(int j = a; j<=b; j++){


        System.out.print("\t"+i*j+"\t");

      }
         System.out.println("\n");

  }
    }
  }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.