Swift Program to Generate Multiplication Table


This tutorial will discuss how to write a Swift program to generate Multiplication Table.

A multiplication table is a list or table of multiples of a specified number. We can create a multiplication table by multiplying a number with whole numbers. Generally, a multiplication table is created upto 10 times but you can create it according to your need.

Below is a demonstration of the same −

Suppose our given input is −

The desired output is −

Following is the multiplication table is 9

9 * 1 = 10 
9 * 2 = 20 
9 * 3 = 30 
9 * 4 = 40 
9 * 5 = 50 
9 * 6 = 60 
9 * 7 = 70 
9 * 8 = 80 
9 * 9 = 90 
9 * 10 = 100

Algorithm to Generate Multiplication Table

  • Step 1 − Define a variable

  • Step 2 − Assign the value of those variables

  • Step 3 − define another variable that will store the output of each multiplied number and print them

  • Step 4 − Iterate the for loop from 1 to 10 and in each iteration we multiply the number 1 to 10 with the given input

  • Step 5 − Print the output

Example 1

The following program shows how to generate a multiplication table.

import Foundation import Glibc var value = 8 for j in 1...10{ // Here, we display the result in tabular form // Using string interpolation print("\(value) * \(j) = \(value * j)") }

Output

8 * 1 = 8 
8 * 2 = 16 
8 * 3 = 24 
8 * 4 = 32 
8 * 5 = 40 
8 * 6 = 48 
8 * 7 = 56 
8 * 8 = 64 
8 * 9 = 72 
8 * 10 = 80

Here in the above code, we create a multiplication table of 8 using for loop. We have iterated the for loop from 1 to 10 and in each iteration we multiply the number 1 to 10 with the given input and display the final result.

Example 2

The following program shows how to generate a multiplication table with user defined input.

import Foundation import Glibc print("Please enter the number -") // Here we convert the enter data into to integer var tablenumber = Int(readLine()!)! print("Please enter the range -") var tableRange = Int(readLine()!)! print("\nEntered number is - ", tablenumber) print("Entered range is-", tableRange) print("Following is the multiplication table is \(tablenumber)") for j in 1...tableRange{ print("\(tablenumber) * \(j) = \(tablenumber * j)") }

STDIN Input

Please enter the number -
19
Please enter the range -
15

Output

Entered number is - 19
Entered range is- 15
Following is the multiplication table is 19
19 * 1 = 19
19 * 2 = 38
19 * 3 = 57
19 * 4 = 76
19 * 5 = 95
19 * 6 = 114
19 * 7 = 133
19 * 8 = 152
19 * 9 = 171
19 * 10 = 190
19 * 11 = 209
19 * 12 = 228
19 * 13 = 247
19 * 14 = 266
19 * 15 = 285

Here we are taking two inputs from the user one is the number of which we find the multiplication table and another is the range, upto which we multiple the numbers or we can say the length of the multiplication table. Now we iterate the for loop from 1… to user input range and in each iteration, we multiply the number 1 to… user input range. In our code the input is number = 19 and range = 15, so the for loop start from 1 to 15 and display the table of 19 till the multiplication of 15.

Updated on: 01-Aug-2022

767 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements