How to calculate the power of any number in the Scala programming language. Lets us define three variable num for number, pow for power, and result in order to store the last result in it. Initialize all the values to 0.
object divinseo{
def main(args: Array[String]){
var num: Double = 0
var pow: Double = 0
var result: Double= 0
Console.println("Enter the number: ")
num=scala.io.StdIn.readInt()
Console.println("Enter the power: ")
pow= scala.io.StdIn.readInt()
result=scala.math.pow(num,pow)
println("Result is : "+result)
}
}
Scala
Take the value of the number and the value of power from the user. Pass both values to the function scala.math.pow(n,p)
, store the result in the result variable.
Output
Enter the number: 4
Enter the power: 5
Result is : 1024.0