Swift – Print Multiplication Table of a Number

In this tutorial, we will learn how to print the multiplication table of a given number in Swift. We will use a simple loop to generate the table and format the output for better readability.


Understanding the Multiplication Table

A multiplication table lists the products of a number multiplied by integers up to a certain range. For example, the multiplication table of 5 up to 10 is:

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

We will use a loop to calculate and print the table dynamically for any given number and range.


Swift Program to Print Multiplication Table

Here’s how to implement a multiplication table in Swift:

</>
Copy
func printMultiplicationTable(of number: Int, upTo range: Int) {
    for i in 1...range {
        let result = number * i
        print("\(number) x \(i) = \(result)")
    }
}

// Example usage
printMultiplicationTable(of: 5, upTo: 10)

Explanation:

  • func printMultiplicationTable(of number: Int, upTo range: Int): A function that takes the number and range as inputs.
  • for i in 1...range: A loop that iterates from 1 to the specified range.
  • let result = number * i: Calculates the product of the number and the current loop index.
  • print("\(number) x \(i) = \(result)"): Prints the multiplication in a formatted string.

Handling Edge Cases

You can handle edge cases, such as when the range is zero or negative, by adding a check before proceeding with the calculation:

</>
Copy
func printMultiplicationTable(of number: Int, upTo range: Int) {
    guard range > 0 else {
        print("The range must be greater than zero.")
        return
    }
    
    for i in 1...range {
        let result = number * i
        print("\(number) x \(i) = \(result)")
    }
}

// Example usage
printMultiplicationTable(of: 7, upTo: 0)

This ensures that invalid ranges are handled gracefully, and users are notified with a message.


Complete Swift Program

Here’s the complete Swift program to print the multiplication table of a number:

main.swift

</>
Copy
import Foundation

// Function to print the multiplication table
func printMultiplicationTable(of number: Int, upTo range: Int) {
    guard range > 0 else {
        print("The range must be greater than zero.")
        return
    }
    
    for i in 1...range {
        let result = number * i
        print("\(number) x \(i) = \(result)")
    }
}

// Example usage
printMultiplicationTable(of: 5, upTo: 10) // Table of 5 up to 10
printMultiplicationTable(of: 7, upTo: 12) // Table of 7 up to 12
printMultiplicationTable(of: 9, upTo: -1) // Invalid range

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
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
7 x 11 = 77
7 x 12 = 84
The range must be greater than zero.
Program ended with exit code: 0

This program dynamically calculates and prints the multiplication table for any number and range, while handling invalid inputs gracefully.

Swift Program to Print Multiplication Table of a Number