Scala program to multiply two numbers using the plus (+) operator

How to multiply two numbers using the plus (+) operator in the Scala programming language. In this program, we are not going to use the multiplication (*) operator.

object divineseo{
    def main(args: Array[String]){
        var n1: Int = 0
        var n2: Int = 0
        var mult: Int = 0
        var count: Int = 0
        
        Console.println("Enter the 1st number : ")
        n1= scala.io.StdIn.readInt()
        
        Console.println("Enter the 2nd number : ")
        n2= scala.io.StdIn.readInt()
        
        count = 1
        while(count <= n2){
            mult= n1 + mult
            count = count + 1
        }
        
        println("Multiplication is "+mult)
    }
}
Scala

Here we declare n1 for number 1, and n2 for number 2, mult variable is to store the last result and count that has to be incremented after each iteration.

Output

Enter the 1st number : 9
Enter the 2nd number : 4
Multiplication is 36

Leave a Comment