Day 13: Multiplication Table

Objective

Today, you will create a program that generates a multiplication table for a number provided by the user. This exercise is a great way to practice working with loops and understand how to perform repetitive tasks programmatically.

For example, if the user inputs 5, the program should display:

5 x 1 = 5  
5 x 2 = 10  
5 x 3 = 15  
...  
5 x 10 = 50  

Why This Challenge Is Important

By solving this challenge, you will:

  1. Learn to Use Loops: Practice using loops to generate repetitive outputs.
  2. Work with Arithmetic Operations: Reinforce your understanding of multiplication.
  3. Learn Formatting Techniques: Practice displaying output in a clear and structured format.

Steps to Solve

1. Understand the Problem

  • Prompt the user to input a number.
  • Use a loop to calculate and display the product of the number with values from 1 to 10.

2. Output Structure

Each line should follow this format:

<number> x <multiplier> = <product>

3. Optional Extensions

  • Allow the user to define the range (e.g., 1 to 20 instead of 1 to 10).
  • Add error handling for invalid inputs (e.g., non-numeric values).

Code Examples

Python Example

# Get input from the user
number = int(input("Enter a number to generate its multiplication table: "))

# Generate multiplication table from 1 to 10
print(f"Multiplication Table for {number}:")
for i in range(1, 11):
    print(f"{number} x {i} = {number * i}")

Java Example

import java.util.Scanner;

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

        // Get input from the user
        System.out.print("Enter a number to generate its multiplication table: ");
        int number = scanner.nextInt();

        // Generate multiplication table from 1 to 10
        System.out.println("Multiplication Table for " + number + ":");
        for (int i = 1; i <= 10; i++) {
            System.out.println(number + " x " + i + " = " + (number * i));
        }
    }
}

JavaScript Example

// Get input from the user
let number = parseInt(prompt("Enter a number to generate its multiplication table:"));

// Generate multiplication table from 1 to 10
console.log(`Multiplication Table for ${number}:`);
for (let i = 1; i <= 10; i++) {
    console.log(`${number} x ${i} = ${number * i}`);
}

Edge Cases to Consider

  1. Zero as Input: A multiplication table for 0 will have all products as 0.
  2. Negative Numbers: Handle multiplication tables for negative numbers correctly (e.g., −5 × 2 = −10)
  3. Non-Numeric Input: Add validation to ensure the input is a valid number.

Extensions to Explore

  1. Custom Range: Let the user define the start and end values for the table (e.g., from 1 to 20).
  2. Display in a Grid: Show the full table (e.g., 1 × 1 to 10 × 10).
  3. Save to a File: Write the table to a text file for future reference.

What You’ve Learned

  • How to use loops to perform repetitive calculations.
  • How to handle user input and dynamically generate output.
  • Techniques to display formatted output cleanly.

Next Steps

In Day 14: Find the Largest Number, you’ll write a program that finds the largest number from a list of numbers provided by the user.