How to multiply two numbers without using the multiple operator (*) in Scala

1 Answer

0 votes
object MultiplyTwoNumbersWithoutUsingMultipleOperator_Scala {
  def multiply(a: Int, b: Int): Int = {
    var mul = 0
    
    for (i <- 1 to a) {
      mul += b
    }
    
    mul
  }

  def main(args: Array[String]): Unit = {
    val a = 5
    val b = 9
    
    println(s"$a * $b = ${multiply(a, b)}")
  }
}



/*
run:

5 * 9 = 45
    
*/

 



answered Aug 31, 2024 by avibootz
...