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 to print the sum (addition), multiplication, subtraction, and division of two numbers....
Write a Java program to print the sum (addition), multiplication, subtraction, and division of two numbers. Start your code by copying/pasting this information into an editor like notepad, notepad++, or IDLE: public class Main { public static void main(String[] args) { // Write your code here } } Sample input: Input first number: 125 Input second number: 24 Sample Output: 125 + 24 = 149 125 - 24 = 101 125 x 24 = 3000 125 / 24 = 5...
Write a program in java which is in main and uses no classes, methods, loops or...
Write a program in java which is in main and uses no classes, methods, loops or if statements Ask the user to enter their first name Ask the user to enter their last name Print their initials followed by periods (ie. Clark Kent = C. K.) Print the number of letters in their last name
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".
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT