Question

In: Computer Science

Write a Java program that uses nested for loops to print a multiplication table as shown...

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.

Solutions

Expert Solution

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();
                }
        }
}


Related Solutions

IN C++ Write a program that uses nested loops to collect data and calculate the average...
IN C++ Write a program that uses nested loops to collect data and calculate the average rainfall over a period of years. The program should first ask the user for the number of years. The outer loop will iterate once for each year. The inner loop will iterate 12 times, once for each month. Each iteration of the inner loop will ask the user for the inches of rainfall for that month. After all iterations, the ­­program should display the...
Write a C++ program that uses nested loops to collect data and calculate the average rainfall...
Write a C++ program that uses nested loops to collect data and calculate the average rainfall over a period of years. The program should first ask the user for the number of years. The outer loop will iterate once for each year. The inner loop will iterate 12 times, once for each month. Each iteration of the inner loop will ask the user for the inches of rainfall for that month. After all iterations, the program should display the number...
Java Program Use for loop 1.) Write a program to display the multiplication table of a...
Java Program Use for loop 1.) Write a program to display the multiplication table of a given integer. Multiplier and number of terms (multiplicand) must be user's input. Sample output: Enter the Multiplier: 5 Enter the number of terms: 3 5x0=0 5x1=5 5x2=10 5x3=15 2 Create a program that will allow the user to input an integer and display the sum of squares from 1 to n. Example, the sum of squares for 10 is as follows: (do not use...
write a program that uses exactly four for loops to print the sequence of letters below...
write a program that uses exactly four for loops to print the sequence of letters below A A A A A A A A A A B B B B B B B C C C C C C E E E E E
Write a java program of a multiplication table of binary numbers using a 2D array of...
Write a java program of a multiplication table of binary numbers using a 2D array of integers.
7. Average Rainfall Design a program that uses nested loops to collect data and calculate the...
7. Average Rainfall Design a program that uses nested loops to collect data and calculate the average rainfall over a period of years. The program should first ask for the number of years. The outer loop will iterate once for each year. The inner loop will iterate twelve times, once for each month. Each iteration of the inner loop will ask the user for the inches of rainfall for that month. After all iterations, the program should display the number...
Design a program that uses Nested loops to collect data and calculate the average rainfall over...
Design a program that uses Nested loops to collect data and calculate the average rainfall over a period of years. The program should first ask for the numbers of years. The outer loop will iterate once for each year. The inner loop will iterate twelve times, once for each month. After all iterations, the program should display the number of months, the total inches of rainfall, and the average rainfall per month for the entire period.
C++ In this lab you will be using nested for loops to print out stars in...
C++ In this lab you will be using nested for loops to print out stars in a Diamond pattern such as this: * *** ***** ******* ********* *********** ************* *************** ************* *********** ********* ******* ***** *** * For example , if number of rows=8 then the above pattern is derived. You are to take the input for the number of lines(rows) from a file named "input_diamond" and output the pattern into both the terminal and an output file named "output_diamond".
Print the following two patterns using nested loops. Using java. Pattern 1 13579 13579 13579 13579...
Print the following two patterns using nested loops. Using java. Pattern 1 13579 13579 13579 13579 Pattern 2 #####1 ### #12 ###123 ##1234 #12345
In this exercise we will practice using nested loops. You will write s program that prompts...
In this exercise we will practice using nested loops. You will write s program that prompts the user to enter an integer between 1 and 9 and displays a pyramid of numbers, as shown in the example below. The program must validate the input given by the user and make sure that: if the user enters a non-integer the program issues an error message and requests the user provides a valid input. if the user does not enter a number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT