In this tutorial, we are going to learn to write a Scala program to calculate the quotient and remainder for a given dividend and divisor.
Before proceeding with writing the Scala program, let us recall some basic terms related to division.
- A dividend is a number that is divided by the divisor.
- The divisor is the factor that divides the dividend.
- The result obtained by the division process is called the quotient
- The number left behind after the completion of the division is called the remainder.
Here is a pictorial representation of the aforementioned terms to have a better understanding.
Here is a mathematical formula for those terms.
Dividend = Divisor × Quotient + Remainder
This can be verified in the division shown above: 13 = 4 * 3 + 1.
Scala Program to Compute Quotient and Remainder
Now that you know the basic terms of division, we shall proceed to write the Scala program.
To calculate the quotient and reminder from the given divider and dividend, we need to use two operators, as follows:
- / -To calculate the quotient
Quotient = dividend/divisor
- % - To calculate the remainder
Reminder = dividend%divisor
Let us start writing the Scala program.
objectDivisonExampleextendsApp {
val dividend = 37
val divisor = 5
val quotient: Int = dividend / divisor
val remainder: Int = dividend % divisor
println("Quotient is : " + quotient)
println("Remainder is : " + remainder)
}
Output:
Quotient is : 7
Remainder is : 2
In the above program, we have taken two input variables, dividend and divisor , for dividend and divisor, respectively. Further, we used the two aforementioned operators, / and %, to compute the quotient and remainder, resp. Please note that the return type of both quotient and remainder is ‘Int’ since both dividend and divisor are ‘Int’ .
Note: If you try to run the same program for a different data type, you need to make sure about the accuracy of the program since the datatypes play an important role in that case.
For example, let us consider a dividend with the type Double . Let us see how the program can change. Please keep an eye on how the data types have changed in the below program:
val dividend: Double = 37.5
val divisor: Int = 5
val quotient: Double = dividend / divisor
val remainder: Double = dividend % divisor
println("Quotient is : " + quotient)
println("Remainder is : " + remainder)
println(s"Checking program accuracy : + ${divisor * quotient + remainder}")
}
Output:
Quotient is : 7.5
Remainder is : 2.5
Checking program accuracy : + 40.0
In the above example, we have updated the type of the dividend to Double . On updating, the type of quotient and reminder also changes to Double .
Just look at the last line of the output that states the accuracy of the program. The accuracy number in the output and the divided defined in the program do not match. So, there is something fishy in the program.
Let's see what is wrong with the above example. Oh, it's the return type of quotient which is making it wrong. It should be Integer while we are getting Double .
Let us make this update and see if the program is correct now:
objectDivisonExampleextendsApp {
val dividend: Double = 37.5
val divisor = 5
val quotient: Int = (dividend / divisor).toInt
val remainder: Double = dividend % divisor
println("Quotient is : " + quotient)
println("Remainder is : " + remainder)
println(s"Checking program accuracy : + ${divisor * quotient + remainder}")
}
Output:
Quotient is : 7
Remainder is : 2.5
Checking program accuracy : + 37.5
Now, we can see that the program is working in the appropriate way and is validated by the computation in the last line which matches the dividend.
Conclusion
That’s all about computing the quotient and remainder with a given dividend and divisor in Scala. Try writing a Scala program where the dividend and divisor are given as the user input and print the corresponding quotient and remainder.
Happy Coding!
Leave a Comment on this Post