How to multiply two integers represented as strings and return the result also as a string in Swift

1 Answer

0 votes
import Foundation

func multiplyStrings(_ num1: String, _ num2: String) -> String {
    // Convert strings to integers
    let n1 = Int(num1) ?? 0
    let n2 = Int(num2) ?? 0

    // Perform multiplication
    let result = n1 * n2

    // Convert result back to string
    return String(result)
}

let num1 = "123"
let num2 = "456"

print("Result: \(multiplyStrings(num1, num2))")



/*
run:

Result: 56088

*/

 



answered Jun 6 by avibootz
...