Problem:
Write an application called MultiplicationTable that asks the user to input a number N then creates a 2 dimensional array of size N X N and stores inside it the table of multiplication up to N.
Output:
Enter N: 4
Outputs:
1 2 3 4
2 4 12 8
3 6 9 12
4 8 12 16
Outputs:
1 2 3 4
2 4 12 8
3 6 9 12
4 8 12 16
Solution:
import java.util.Scanner;
public class Problem1 {
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
int[][] table = new int[num][num];
for(int i=0; i<num; i++)
{
for(int j=0; j<num; j++)
{
table[i][j] = (i+1)*(j+1);
}
}
for(int i=0; i<num; i++)
{
for(int j=0; j<num; j++)
{
System.out.print(table[i][j] + "\t");
}
System.out.println();
}
}
}
No comments :
Post a Comment