In: Computer Science
Write a Java program that uses nested for loops to print a multiplication table as shown below.
Make sure to include the table headings and separators as shown.
The values in the body of the table should be computed using the values in the heading
e.g. row 1 column 3 is 1 times 3.
Please find your solution below an if any doubt comment and do upvote.
import java.util.*;
public class Main
{
public static void main(String[] args) {
//Scanner Object for input
Scanner sc=new Scanner(System.in);
int n;
//take user input
System.out.print("Enter the number: ");
n=sc.nextInt();
System.out.print("* |");
for(int i=1;i<=n;i++){
System.out.printf("%4d",i);
}
System.out.println();
System.out.print("----");
for(int i=1;i<=n;i++){
System.out.print("----");
}
//nested loop to print the table
System.out.println();
for(int i=1;i<=n;i++){
System.out.printf(" %d |", i);
for(int j=1;j<=n;j++){
System.out.printf("%4d",i*j);
}
System.out.println();
}
}
}