Arithmetic/Integer: Difference between revisions

Content added Content deleted
(Added Kotlin)
Line 1,710: Line 1,710:
a div b = 0
a div b = 0
a rem b = 4</pre>
a rem b = 4</pre>

=={{header|Kotlin}}==
<lang scala>// version 1.0.5-2

fun main(args: Array<String>) {
val r = Regex("""-?\d+[ ]+-?\d+""")
while(true) {
print("Enter two integers separated by space(s) or q to quit: ")
val input: String = readLine()!!.trim()
if (input == "q" || input == "Q") break;
if (!input.matches(r)) {
println("Invalid input, try again")
continue
}
val index = input.lastIndexOf(' ')
val a = input.substring(0, index).trimEnd().toLong()
val b = input.substring(index + 1).toLong()
println("$a + $b = ${a + b}")
println("$a - $b = ${a - b}")
println("$a * $b = ${a * b}")
if (b != 0L) {
println("$a / $b = ${a / b}") // rounds towards zero
println("$a % $b = ${a % b}") // if non-zero, matches sign of first operand
}
else {
println("$a / $b = undefined")
println("$a % $b = undefined")
}
val d = Math.pow(a.toDouble(), b.toDouble())
print("$a ^ $b = ")
if (d % 1.0 == 0.0) {
if (d >= Long.MIN_VALUE.toDouble() && d <= Long.MAX_VALUE.toDouble())
println("${d.toLong()}")
else
println("out of range")
}
else if (!d.isFinite())
println("not finite")
else
println("not integral")
println()
}
}</lang>

{{out}}
<pre>
Enter two integers separated by space(s) or q to quit: 2 63
2 + 63 = 65
2 - 63 = -61
2 * 63 = 126
2 / 63 = 0
2 % 63 = 2
2 ^ 63 = 9223372036854775807

Enter two integers separated by space(s) or q to quit: -3 50
-3 + 50 = 47
-3 - 50 = -53
-3 * 50 = -150
-3 / 50 = 0
-3 % 50 = -3
-3 ^ 50 = out of range

Enter two integers separated by space(s) or q to quit: q
</pre>


=={{header|LabVIEW}}==
=={{header|LabVIEW}}==