Multiplication Table

beginner Programming Basics

Problem

Read an integer n and print its multiplication table from 1 to 10. Each line has the form n x i = result.

Constraints

  • -10^6 <= n <= 10^6

Examples

Input

5

Output

5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

Solutions

Best approachTime O(1)Space O(1)

A for loop with a counter from 1 to 10 builds each line. The loop counter is the multiplier.

Same code works in Java810172125Basic features only, so nothing changes between versions.
/*
 * Problem : Print the multiplication table of a number.
 * Input   : One integer n.
 * Output  : 10 lines in the form n x i = result.
 * Approach: Use a loop from 1 to 10.
 * Time    : O(1)
 * Space   : O(1)
 * Java    : 8 or newer.
 */
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int n = new Scanner(System.in).nextInt();

        // Step 1: Repeat for the multipliers 1, 2, 3, ... up to 10.
        for (int multiplier = 1; multiplier <= 10; multiplier++) {
            // Step 2: Multiply the number by the multiplier. Example: 5 * 3 = 15.
            int result = n * multiplier;

            // Step 3: Print one line of the table.
            System.out.println(n + " x " + multiplier + " = " + result);
        }
    }
}
Hints (1)
  1. How many times must the loop run?