In Java. Write a program that reads-in a times table-number. The program, using this table-number will produce the following report (as shown). The first item in the report is a number with starting value 1. Second column is the word “ X ” (representing the times symbol). Third column is the table-number (itself). Following is an equal sign “ = “ (representing a result). Last column is the result of the operation for that line or row which is the calculation between number times table-number. The process will repeat 20 times or 20 rows.
Use printf to format the output.
Example of how the input and output should look like:
Enter a times table number: 7 Number Times Table Equals Result 1 X 7 = 7 2 X 7 = 14 3 X 7 = 21 . .
|
Use the flowchart below to write the Java Program that generates the Times Table. (Test the flowchart or the pseudo code for accuracy before writing Java Code)
Java program that generates the times table.
Step-1: Read the integer whose table we need to print
Step-2: Use for loop to iterate and print the table from i=1 to i<=20 because we need the table to be printed till 20. Here we are asked to use printf statement to format the output. The printf() function allows the programmer to print multiple objects at the same time.The syntax to use the printf statemement is shown below:
Syntax: System.out.printf(format, args);
The format parameter specifies the formatting rules that begin with % and the args is the arguments referenced by the format specifiers in the format string. The format specifiers %d prints an integer, %c prints a character, %s prints a string.
Note: Please refer to the screenshot of the code to understand the indentation of the code. Also the code is explained in the screenshot.
Java Code:
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int i, n;
Scanner s = new Scanner(System.in);
System.out.print("Enter a times table number:");
n=s.nextInt();
System.out.println("Number Times Table Equals
Result");
for(i=1; i <= 20; i++)
{
System.out.printf( "%d \t %c \t %d \t %c \t %d
\n",i,'x',n,'=',i*n);
}
}
}
The complete program along with the explaination and output is shown below:
Output-1:
Output-2:
Get Answers For Free
Most questions answered within 1 hours.