0

im a beginner here. Ive been stuck on a problem for some time now. Practicing in playground and i need to make a multiplication table. basically, if i input 3, i want the table to read

1 2 3
2 4 6
3 6 9

Im confused on the loop for this though. Any help please?

Code so far

var x = 3
var width = 1

for x in 1...x {
    for width in 1...width {
        print(x, width*2)
    }
}

this code prints

1 2
2 2
3 2
Rakesha Shastri
  • 11,053
  • 3
  • 37
  • 50

3 Answers3

4

You could do it like this.

func multiplicationTable(till limit: Int) {
    for i in 1...limit {
        for j in 1...limit {
            print(i * j, terminator: "\t")
        }
        print("")
    }
}

multiplcationTable(till: 5)

Output

 1    2   3   4   5   
 2    4   6   8   10  
 3    6   9   12  15  
 4    8   12  16  20  
 5    10  15  20  25  
Rakesha Shastri
  • 11,053
  • 3
  • 37
  • 50
  • perfect - i will accept the answer as soon as it lets me. thanks – Coding while Loading Dec 13 '18 at 10:52
  • The inner loop could be replaced by `print((1...limit).map { String(i * $0) }.joined(separator: "\t"))` – ielyamani Dec 13 '18 at 11:00
  • @Carpsen90 yes. That is a good option. I was just amused by the existence of additional parameters for the print statement. So i think i'll keep that. Also, it first creates an array, then iterates it, while my answer would just iterate it once? – Rakesha Shastri Dec 13 '18 at 11:02
  • @Carpsen90 nice. You may edit the answer or post another one if you'd like. I'd personally like to keep this one for the simplicity for such a simple question. – Rakesha Shastri Dec 13 '18 at 11:06
  • Can you make the separator in the print statement be the tab character instead of listing it explicitly? I’m on mobile and can’t try that. – vacawama Dec 13 '18 at 11:14
  • 1
    @vacawama the separator works if there are multiple elements in the same print statment. Check [this](https://stackoverflow.com/a/35695525/7734643) out. Oh wait did you mean this? (Edited answer) – Rakesha Shastri Dec 13 '18 at 11:15
  • 1
    Thanks for the info. How about eliminating the separator and using tab as terminator? – vacawama Dec 13 '18 at 11:19
  • 1
    They mean this `print(i * j, terminator: "\t")` – ielyamani Dec 13 '18 at 11:21
2

If conciseness is paramount:

let x = 3
let range = 1...x
for i in range {
    print(range.map { String(i * $0) }.joined(separator: "\t"))
}
ielyamani
  • 17,807
  • 10
  • 55
  • 90
1

You can store the multiplication table in a 2D array of Ints. First, you can populate the first row and first column with numbers from 1 to the size of the multiplication table. Then for each element in the remaining empty positions, you just need to multiply the first element of the same row and the first element of the same column that the element resides in.

func multiplicationTable(ofSize n:Int) -> [[Int]] {
    var table = Array(repeating: Array(repeating: 0, count: n), count: n)
    table[0] = Array(1...n)
    for i in 1..<n {
        table[i][0] = i+1
        for j in 1..<n {
            table[i][j] = table[i][0] * table[0][j]
        }
    }
    return table
}

multiplicationTable(ofSize: 5).forEach { row in
    print(row,"\n")
}

Output:

[1, 2, 3, 4, 5]

[2, 4, 6, 8, 10]

[3, 6, 9, 12, 15]

[4, 8, 12, 16, 20]

[5, 10, 15, 20, 25]

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116