Arithmetic/Integer: Difference between revisions

Content added Content deleted
m (→‎{{header|Icon}}: Icon+Unicon header simplification)
m (→‎{{header|Python}}: added power operation)
Line 1,207: Line 1,207:
print "Remainder: %d" % (x % y) # same sign as second operand
print "Remainder: %d" % (x % y) # same sign as second operand
print "Quotient: %d with Remainder: %d" % divmod(x, y)
print "Quotient: %d with Remainder: %d" % divmod(x, y)
print "Power: %d" % x**y


## Only used to keep the display up when the program ends
## Only used to keep the display up when the program ends
Line 1,235: Line 1,236:
=== Python 3.0 compatible code ===
=== Python 3.0 compatible code ===
<lang python>def arithmetic(x, y):
<lang python>def arithmetic(x, y):
for op in "+ - * // %".split():
for op in "+ - * // % **".split():
expr = "%(x)s %(op)s %(y)s" % vars()
expr = "%(x)s %(op)s %(y)s" % vars()
print("%s\t=> %s" % (expr, eval(expr)))
print("%s\t=> %s" % (expr, eval(expr)))
Line 1,248: Line 1,249:
12 // 8 => 1
12 // 8 => 1
12 % 8 => 4
12 % 8 => 4
12 ** 8 => 429981696
Number 1: 20
Number 1: 20
Number 2: 4
Number 2: 4
Line 1,254: Line 1,256:
20 * 4 => 80
20 * 4 => 80
20 // 4 => 5
20 // 4 => 5
20 % 4 => 0</pre>
20 % 4 => 0
20 ** 4 => 160000</pre>


=={{header|R}}==
=={{header|R}}==