Arithmetic/Integer

From Rosetta Code
Task
Arithmetic/Integer
You are encouraged to solve this task according to the task description, using any language you may know.

Basic Data Operation
This is a basic data operation. It represents a fundamental action on a basic data type.

You may see other such operations in the Basic Data Operations category, or:

Integer Operations
Arithmetic | Comparison

Boolean Operations
Bitwise | Logical

String Operations
Concatenation | Interpolation | Comparison | Matching

Memory Operations
Pointers & references | Addresses

Task

Get two integers from the user,   and then (for those two integers), display their:

  •   sum
  •   difference
  •   product
  •   integer quotient
  •   remainder
  •   exponentiation   (if the operator exists)


Don't include error handling.

For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.).

For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.

Contents

0815[edit]

 
|~>|~#:end:>
<:61:x<:3d:=<:20:$==$~$=${~>%<:2c:~$<:20:~$
<:62:x<:3d:=<:20:$==$~$=${~>%<:a:~$$
<:61:x<:2b:=<:20:$==$~$=$<:62:x<:3d:=<:20:$==$~$=${x{x~>~>~+%<:a:~$
<:61:x<:2d:=<:20:$==$~$=$<:62:x<:3d:=<:20:$==$~$=${x{x~>~>~-%<:a:~$
<:61:x<:2a:=<:20:$==$~$=$<:62:x<:3d:=<:20:$==$~$=${x{x~>~>~*%<:a:~$
<:61:x<:2f:=<:20:$==$~$=$<:62:x<:3d:=<:20:$==$~$=${x{x~>~>~/%<:a:~$
<:61:x<:25:=<:20:$==$~$=$<:62:x<:3d:=<:20:$==$~$=${x{x~>~>~/=%<:a:~$
{~>>{x<:1:-^:u:
<:61:x<:5e:=<:20:$==$~$$=$<:62:x<:3D:=<:20:$==$~$=${{~%#:end:
}:u:=>{x{=>~*>{x<:2:-#:ter:
}:ml:x->{x{=>~*>{x<:1:-#:ter:^:ml:
}:ter:<:61:x<:5e:=<:20:$==$~$$=$<:62:x<:3D:=<:20:$==$~$=${{~%
 
Output:
a = 6, b = 4

a + b = A
a - b = 2
a * b = 18
a / b = 1
a % b = 2
a ^^ b = 510

360 Assembly[edit]

From the principles of operation: Operands are signed and 32 bits long. Negative quantities are held in two's-complement form.
Multiplication:
The product of the multiplier (the second operand) and the multiplicand (the first operand) replaces the multiplicand. Both multiplier and multiplicand are 32-bit signed integers. The product is always a 64-bit signed integer and occupies an even/odd register pair.
Division:
The dividend (first operand) is divided by the divisor (second operand) and replaced by the quotient and remainder. The dividend is a 64-bit signed integer and occupies the even/odd pair of registers. A 32-bit signed remainder and a 32-bit signed quotient replace the dividend in the even-numbered and odd-numbered registers, respectively. The sign of the quotient is determined by the rules of algebra. The remainder has the same sign as the dividend.

*        Arithmetic/Integer        04/09/2015
ARITHINT CSECT
USING ARITHINT,R12
LR R12,R15
ADD L R1,A
A R1,B r1=a+b
XDECO R1,BUF
MVI BUF,C'+'
XPRNT BUF,12
SUB L R1,A
S R1,B r1=a-b
XDECO R1,BUF
MVI BUF,C'-'
XPRNT BUF,12
MUL L R1,A
M R0,B r0r1=a*b
XDECO R1,BUF so r1 has the lower part
MVI BUF,C'*'
XPRNT BUF,12
DIV L R0,A
SRDA R0,32 to shift the sign
D R0,B r1=a/b and r0 has the remainder
XDECO R1,BUF so r1 has quotient
MVI BUF,C'/'
XPRNT BUF,12
MOD L R0,A
SRDA R0,32 to shift the sign
D R0,B r1=a/b and r0 has the remainder
XDECO R0,BUF so r0 has the remainder
MVI BUF,C'R'
XPRNT BUF,12
RETURN XR R15,R15
BR R14
CNOP 0,4
A DC F'53'
B DC F'11'
BUF DC CL12' '
YREGS
END ARITHINT

Inputs are in the code: a=53, b=11

Output:
+         64
-         42
*        583
/          4
R          9

6502 Assembly[edit]

Code is called as a subroutine (i.e. JSR Arithmetic). Specific OS/hardware routines for user input and printing are left unimplemented.

Arithmetic:	PHA			;push accumulator and X register onto stack
TXA
PHA
JSR GetUserInput ;routine not implemented
;two integers now in memory locations A and B
;addition
LDA A
CLC
ADC B
JSR DisplayAddition ;routine not implemented
 
;subtraction
LDA A
SEC
SBC B
JSR DisplaySubtraction ;routine not implemented
 
;multiplication - overflow not handled
LDA A
LDX B
Multiply: CLC
ADC A
DEX
BNE Multiply
JSR DisplayMultiply ;routine not implemented
 
;division - rounds up
LDA A
LDX #0
SEC
Divide: INX
SBC B
BCS Divide
TXA ;get result into accumulator
JSR DisplayDivide ;routine not implemented
 
;modulus
LDA A
SEC
Modulus: SBC B
BCS Modulus
ADC B
JSR DisplayModulus ;routine not implemented
 
PLA ;restore accumulator and X register from stack
TAX
PLA
RTS ;return from subroutine

The 6502 has no opcodes for multiplication, division, or modulus; the routines for multiplication, division, and modulus given above can be heavily optimized at the expense of some clarity.

ABAP[edit]

report zz_arithmetic no standard page heading.
 
" Read in the two numbers from the user.
selection-screen begin of block input.
parameters: p_first type i,
p_second type i.
selection-screen end of block input.
 
" Set the text value that is displayed on input request.
at selection-screen output.
%_p_first_%_app_%-text = 'First Number: '.
%_p_second_%_app_%-text = 'Second Number: '.
 
end-of-selection.
data: lv_result type i.
lv_result = p_first + p_second.
write: / 'Addition:', lv_result.
lv_result = p_first - p_second.
write: / 'Substraction:', lv_result.
lv_result = p_first * p_second.
write: / 'Multiplication:', lv_result.
lv_result = p_first div p_second.
write: / 'Integer quotient:', lv_result. " Truncated towards zero.
lv_result = p_first mod p_second.
write: / 'Remainder:', lv_result.

ACL2[edit]

 
:set-state-ok t
 
(defun get-two-nums (state)
(mv-let (_ a state)
(read-object *standard-oi* state)
(declare (ignore _))
(mv-let (_ b state)
(read-object *standard-oi* state)
(declare (ignore _))
(mv a b state))))
 
(defun integer-arithmetic (state)
(mv-let (a b state)
(get-two-nums state)
(mv state
(progn$ (cw "Sum: ~x0~%" (+ a b))
(cw "Difference: ~x0~%" (- a b))
(cw "Product: ~x0~%" (* a b))
(cw "Quotient: ~x0~%" (floor a b))
(cw "Remainder: ~x0~%" (mod a b))))))

Ada[edit]

with Ada.Text_Io;
with Ada.Integer_Text_IO;
 
procedure Integer_Arithmetic is
use Ada.Text_IO;
use Ada.Integer_Text_Io;
 
A, B : Integer;
begin
Get(A);
Get(B);
Put_Line("a+b = " & Integer'Image(A + B));
Put_Line("a-b = " & Integer'Image(A - B));
Put_Line("a*b = " & Integer'Image(A * B));
Put_Line("a/b = " & Integer'Image(A / B));
Put_Line("a mod b = " & Integer'Image(A mod B)); -- Sign matches B
Put_Line("remainder of a/b = " & Integer'Image(A rem B)); -- Sign matches A
Put_Line("a**b = " & Integer'Image(A ** B));
 
end Integer_Arithmetic;

Aikido[edit]

var a = 0
var b = 0
stdin -> a // read int from stdin
stdin -> b // read int from stdin
 
println ("a+b=" + (a + b))
println ("a-b=" + (a - b))
println ("a*b=" + (a * b))
println ("a/b=" + (a / b))
println ("a%b=" + (a % b))

ALGOL 68[edit]

Translation of: C
Works with: ALGOL 68 version Revision 1 - no extensions to language used
Works with: ALGOL 68G version Any - tested with release 1.18.0-9h.tiny
main:(
LONG INT a=355, b=113;
printf(($"a+b = "gl$, a + b));
printf(($"a-b = "gl$, a - b));
printf(($"a*b = a×b = "gl$, a * b));
printf(($"a/b = "gl$, a / b));
printf(($"a OVER b = a%b = a÷b = "gl$, a % b));
printf(($"a MOD b = a%*b = a%×b = a÷×b = a÷*b = "gl$, a %* b));
printf(($"a UP b = a**b = a↑b = "gl$, a ** b))
)
Output:
a+b =                                 +468
a-b =                                 +242
a*b = a×b =                               +40115
a/b = +3.141592920353982300884955752e  +0
a OVER b = a%b = a÷b =                                   +3
a MOD b = a%*b = a%×b = a÷×b = a÷*b =                                  +16
a UP b = a**b = a↑b = +1.499007808785573768814747570e+288

ALGOL 68R has the curious (and consequently non-standard) '/:=' operator. This operator is equivalent to the OVERAB operator of the revised report, except it delivers the remainder as a result. So a '/:=' b sets a to the quotient of a%b and returns the remainder of a%b as a result. Note that it must be "stropped" i.e. enclosed in single quotes. eg.

INT quotient:=355, remainder;
remainder := quotient '/:=' 113;

Giving a quotient of 3, and a remainder of 16.

AmigaE[edit]

PROC main()
DEF a, b, t
WriteF('A = ')
ReadStr(stdin, t)
a := Val(t)
WriteF('B = ')
ReadStr(stdin, t)
b := Val(t)
WriteF('A+B=\d\nA-B=\d\n', a+b, a-b)
WriteF('A*B=\d\nA/B=\d\n', a*b, a/b)
/* * and / are 16 bit ops; Mul and Div are 32bit ops */
WriteF('A*B=\d\nA/B=\d\n', Mul(a,b), Div(a,b))
WriteF('A mod B =\d\n', Mod(a,b))
ENDPROC

AutoHotkey[edit]

The quotient rounds towards 0 if both inputs are integers or towards negative infinity if either input is floating point. The sign of the remainder is always the same as the sign of the first parameter (dividend).

Gui, Add, Edit, va, 5
Gui, Add, Edit, vb, -3
Gui, Add, Button, Default, Compute
Gui, Show
Return
 
ButtonCompute:
Gui, Submit
MsgBox,%
(Join`s"`n"
a "+" b " = " a+b
a "-" b " = " a-b
a "*" b " = " a*b
a "//" b " = " a//b " remainder " Mod(a,b)
a "**" b " = " a**b
)
; fallthrough
GuiClose:
ExitApp

AWK[edit]

/^[ \t]*-?[0-9]+[ \t]+-?[0-9]+[ \t]*$/ {
print "add:", $1 + $2
print "sub:", $1 - $2
print "mul:", $1 * $2
print "div:", int($1 / $2) # truncates toward zero
print "mod:", $1 % $2 # same sign as first operand
print "exp:", $1 ^ $2
exit }

For division and modulus, Awk should act like C.

Exponentiation's note: With nawk or gawk, $1 ** $2 acts like $1 ^ $2. With mawk, $1 ** $2 is a syntax error. Nawk allows **, but its manual page only has ^. Gawk's manual warns, "The POSIX standard only specifies the use of `^' for exponentiation. For maximum portability, do not use the `**' operator."

BASIC[edit]

Works with: QuickBasic version 4.5
FUNCTION math(a!, b!)
PRINT a + b
PRINT a - b
PRINT a * b
PRINT a / b
PRINT a MOD b
END FUNCTION

Truncate towards: 0

Remainder sign matches: first operand

BASIC256[edit]

 
input "enter a number ?", a
input "enter another number ?", b
 
print "addition " + a + " + " + b + " = " + (a + b)
print "subtraction " + a + " - " + b + " = " + (a - b)
print "multiplication " + a + " * " + b + " = " + (a * b)
print "integer division " + a + " \ " + b + " = " + (a \ b)
print "remainder or modulo " + a + " % " + b + " = " + (a % b)
print "power " + a + " ^ " + b + " = " + (a ^ b)
 

Batch File[edit]

Works with: Windows NT version 4 or later (includes Windows XP and onward)
 
@echo off
set /P A=Enter 1st Number :
set /P B=Enter 2nd Number :
set D=%A% + %B% & call :printC
set D=%A% - %B% & call :printC
set D=%A% * %B% & call :printC
set D=%A% / %B% & call :printC & rem truncates toward 0
set D=%A% %% %B% & call :printC & rem matches sign of 1st operand
exit /b
 
:printC
set /A C=%D%
echo %D% = %C%
 

BBC BASIC[edit]

      INPUT "Enter the first integer: " first%
INPUT "Enter the second integer: " second%
 
PRINT "The sum is " ; first% + second%
PRINT "The difference is " ; first% - second%
PRINT "The product is " ; first% * second%
PRINT "The integer quotient is " ; first% DIV second% " (rounds towards 0)"
PRINT "The remainder is " ; first% MOD second% " (sign matches first operand)"
PRINT "The first raised to the power of the second is " ; first% ^ second%

bc[edit]

define f(a, b) {
"add: "; a + b
"sub: "; a - b
"mul: "; a * b
"div: "; a / b /* truncates toward zero */
"mod: "; a % b /* same sign as first operand */
"pow: "; a ^ b
}

Befunge[edit]

&&00p"=A",,:."=B ",,,00g.55+,v
v,+55.+g00:,,,,"A+B="<
>"=B-A",,,,:00g-.55+,v
v,+55.*g00:,,,,"A*B="<
>"=B/A",,,,:00g/.55+,v
@,+55.%g00,,,,"A%B="<

Bracmat[edit]

The remainder returned by mod is non-negative. Furthermore, div$(!a.!d)*!d+mod$(!a.!d):!a for all integer !a and !d, !d:~0.

  ( enter
= put$"Enter two integer numbers, separated by space:"
& get':(~/#?k_~/#?m|quit:?k)
| out
$ "You must enter two integer numbers! Enter \"quit\" if you don't know how to do that."
& !enter
)
& !enter
& !k:~quit
& out$("You entered" !k and !m ". Now look:")
& out$("Sum:" !k+!m)
& out$("Difference:" !k+-1*!m)
& out$("Product:" !k*!m)
& out$("Integer division:" div$(!k.!m))
& out$("Remainder:" mod$(!k.!m))
& out$("Exponentiation:" !k^!m)
& done;
 

Brat[edit]

Inspired by the second VBScript version.

x = ask("First number: ").to_i
y = ask("Second number: ").to_i
 
#Division uses floating point
#Remainder uses sign of right hand side
[:+ :- :* :/ :% :^].each { op |
p "#{x} #{op} #{y} = #{x.call_method op, y}"

C[edit]

#include <stdio.h>
#include <stdlib.h>
 
int main(int argc, char *argv[])
{
int a, b;
if (argc < 3) exit(1);
b = atoi(argv[--argc]);
if (b == 0) exit(2);
a = atoi(argv[--argc]);
printf("a+b = %d\n", a+b);
printf("a-b = %d\n", a-b);
printf("a*b = %d\n", a*b);
printf("a/b = %d\n", a/b); /* truncates towards 0 (in C99) */
printf("a%%b = %d\n", a%b); /* same sign as first operand (in C99) */
return 0;
}

C++[edit]

#include <iostream>
 
int main()
{
int a, b;
std::cin >> a >> b;
std::cout << "a+b = " << a+b << "\n";
std::cout << "a-b = " << a-b << "\n";
std::cout << "a*b = " << a*b << "\n";
std::cout << "a/b = " << a/b << ", remainder " << a%b << "\n";
return 0;
}

C#[edit]

using System;
 
class Program
{
static void Main(string[] args)
{
int a = Convert.ToInt32(args[0]);
int b = Convert.ToInt32(args[1]);
 
Console.WriteLine("{0} + {1} = {2}", a, b, a + b);
Console.WriteLine("{0} - {1} = {2}", a, b, a - b);
Console.WriteLine("{0} * {1} = {2}", a, b, a * b);
Console.WriteLine("{0} / {1} = {2}", a, b, a / b); // truncates towards 0
Console.WriteLine("{0} % {1} = {2}", a, b, a % b); // matches sign of first operand
Console.WriteLine("{0} to the power of {1} = {2}", a, b, Math.Pow(a, b));
}
}
Output:
5 + 3 = 8
5 - 3 = 2
5 * 3 = 15
5 / 3 = 1
5 % 3 = 2
5 to the power of 3 = 125

Chef[edit]

Number Soup.
 
Only reads single values.
 
Ingredients.
1 g Numbers
3 g Water
5 g Soup
 
Method.
Take Numbers from refrigerator.
Take Soup from refrigerator.
Put Numbers into 1st mixing bowl.
Add Soup into the 1st mixing bowl.
Pour contents of the 1st mixing bowl into 1st baking dish.
Clean 1st mixing bowl.
Put Numbers into 1st mixing bowl.
Remove Soup from 1st mixing bowl.
Pour contents of the 1st mixing bowl into 2nd baking dish.
Clean 1st mixing bowl.
Put Numbers into 1st mixing bowl.
Combine Soup into 1st mixing bowl.
Pour contents of the 1st mixing bowl into 3rd baking dish.
Clean 1st mixing bowl.
Put Numbers into 1st mixing bowl.
Divide Soup into 1st mixing bowl.
Pour contents of the 1st mixing bowl into 4th baking dish.
Clean 1st mixing bowl.
Put Water into 1st mixing bowl.
Verb the Soup.
Combine Numbers into 1st mixing bowl.
Verb the Soup until verbed.
Pour contents of the 1st mixing bowl into 5th baking dish.
Clean 1st mixing bowl.
 
Serves 5.

Clipper[edit]

procedure Test( a, b )
? "a+b", a + b
? "a-b", a - b
? "a*b", a * b
// The quotient isn't integer, so we use the Int() function, which truncates it downward.
? "a/b", Int( a / b )
// Remainder:
? "a%b", a % b
// Exponentiation is also a base arithmetic operation
? "a**b", a ** b
return

Clojure[edit]

(defn myfunc []
(println "Enter x and y")
(let [x (read), y (read)]
(doseq [op '(+ - * / Math/pow rem)]
(let [exp (list op x y)]
(printf "%s=%s\n" exp (eval exp))))))
user=> (myfunc)
Enter x and y
3
6
(+ 3 6)=9
(- 3 6)=-3
(* 3 6)=18
(/ 3 6)=1/2
(Math/pow 3 6)=729.0
(rem 3 6)=3
nil

COBOL[edit]

       IDENTIFICATION DIVISION.
PROGRAM-ID. Int-Arithmetic.
 
DATA DIVISION.
WORKING-STORAGE SECTION.
 
01 A PIC S9(10).
01 B PIC S9(10).
01 Result PIC S9(10).
 
PROCEDURE DIVISION.
DISPLAY "First number: " WITH NO ADVANCING
ACCEPT A
DISPLAY "Second number: " WITH NO ADVANCING
ACCEPT B
 
* *> Note: The various ADD/SUBTRACT/etc. statements can be
* *> replaced with COMPUTE statements, which allow those
* *> operations to be defined similarly to other languages,
* *> e.g. COMPUTE Result = A + B
 
ADD A TO B GIVING Result
DISPLAY "A + B = " Result
 
SUBTRACT B FROM A GIVING Result
DISPLAY "A - B = " Result
 
MULTIPLY A BY B GIVING Result
DISPLAY "A * B = " Result
 
* *> Division here truncates towards zero. DIVIDE can take a
* *> ROUNDED clause, which will round the result to the nearest
* *> integer.
DIVIDE A BY B GIVING Result
DISPLAY "A / B = " Result
 
COMPUTE Result = A ^ B
DISPLAY "A ^ B = " Result
 
* *> Matches sign of first argument.
DISPLAY "A % B = " FUNCTION REM(A, B)
 
GOBACK
.

Common Lisp[edit]

(defun arithmetic (&optional (a (read *query-io*)) (b (read *query-io*)))
(mapc
(lambda (op)
(format t "~a => ~a~%" (list op a b) (funcall (symbol-function op) a b)))
'(+ - * mod rem floor ceiling truncate round expt))
(values))

Common Lisp's integer division functions are floor, ceiling, truncate, and round. They differ in how they round their quotient.

The function rounds its quotient towards
floor negative infinity
ceiling positive infinity
truncate zero
round the nearest integer (preferring the even integer if the mathematical quotient is equidistant from two integers)

Each function also returns a remainder as its secondary value, such that

 quotient * divisor + remainder = dividend .

(mod a b) and (rem a b) return numbers equal to the secondary values of (floor a b) and (truncate a b), respectively.

Component Pascal[edit]

Works with Gardens Point Component Pascal

 
MODULE Arithmetic;
IMPORT CPmain,Console,RTS;
 
VAR
x,y : INTEGER;
arg : ARRAY 128 OF CHAR;
status : BOOLEAN;
 
 
PROCEDURE Error(IN str : ARRAY OF CHAR);
BEGIN
Console.WriteString(str);Console.WriteLn;
HALT(1)
END Error;
 
 
BEGIN
IF CPmain.ArgNumber() < 2 THEN Error("Give me two integers!") END;
CPmain.GetArg(0,arg); RTS.StrToInt(arg,x,status);
IF ~status THEN Error("Can't convert '"+arg+"' to Integer") END;
CPmain.GetArg(1,arg); RTS.StrToInt(arg,y,status);
IF ~status THEN Error("Can't convert '"+arg+"' to Integer") END;
Console.WriteString("x + y >");Console.WriteInt(x + y,6);Console.WriteLn;
Console.WriteString("x - y >");Console.WriteInt(x - y,6);Console.WriteLn;
Console.WriteString("x * y >");Console.WriteInt(x * y,6);Console.WriteLn;
Console.WriteString("x / y >");Console.WriteInt(x DIV y,6);Console.WriteLn;
Console.WriteString("x MOD y >");Console.WriteInt(x MOD y,6);Console.WriteLn;
END Arithmetic.
 

command: cprun Arithmetic 12 23

Output:
x + y >    35
x - y >   -11
x * y >   276
x / y >     0
x MOD y >    12

Works with BlackBox Component Builder

 
MODULE Arithmetic;
IMPORT StdLog,DevCommanders,TextMappers;
 
PROCEDURE DoArithmetic(x,y: INTEGER);
BEGIN
StdLog.String("x + y >");StdLog.Int(x + y);StdLog.Ln;
StdLog.String("x - y >");StdLog.Int(x - y);StdLog.Ln;
StdLog.String("x * y >");StdLog.Int(x * y);StdLog.Ln;
StdLog.String("x / y >");StdLog.Int(x DIV y);StdLog.Ln;
StdLog.String("x MOD y >");StdLog.Int(x MOD y);StdLog.Ln;
END DoArithmetic;
 
PROCEDURE Go*;
VAR
params: DevCommanders.Par;
s: TextMappers.Scanner;
p : ARRAY 2 OF INTEGER;
current: INTEGER;
BEGIN
current := 0;
params := DevCommanders.par;
s.ConnectTo(params.text);
s.SetPos(params.beg);
s.Scan;
WHILE(~s.rider.eot) DO
IF (s.type = TextMappers.int) THEN
p[current] := s.int; INC(current);
END;
s.Scan;
END;
IF current = 2 THEN DoArithmetic(p[0],p[1]) END;
END Go;
END Arithmetic.
 

Command: Arithmetic.Go 12 23 ~

Output:
x + y > 35
x - y > -11
x * y > 276
x / y > 0
x MOD y > 12

D[edit]

import std.stdio, std.string, std.conv;
 
void main() {
int a = 10, b = 20;
try {
a = readln().strip().to!int();
b = readln().strip().to!int();
} catch (StdioException e) {}
writeln("a = ", a, ", b = ", b);
 
writeln("a + b = ", a + b);
writeln("a - b = ", a - b);
writeln("a * b = ", a * b);
writeln("a / b = ", a / b);
writeln("a % b = ", a % b);
writeln("a ^^ b = ", a ^^ b);
}
Output:
a = -16, b = 5
a + b = -11
a - b = -21
a * b = -80
a / b = -3
a % b = -1
a ^^ b = -1048576

Shorter Version[edit]

Same output.

import std.stdio, std.string, std.conv, std.typetuple;
 
void main() {
int a = -16, b = 5;
try {
a = readln().strip().to!int();
b = readln().strip().to!int();
} catch (StdioException e) {}
writeln("a = ", a, ", b = ", b);
 
foreach (op; TypeTuple!("+", "-", "*", "/", "%", "^^"))
mixin(`writeln("a ` ~ op ~ ` b = ", a` ~ op ~ `b);`);
}

Division and modulus are defined as in C99.

dc[edit]

[Enter 2 integers on 1 line.
Use whitespace to separate. Example: 2 3
Use underscore for negative integers. Example: _10
]P ? sb sa
[add: ]P la lb + p sz
[sub: ]P la lb - p sz
[mul: ]P la lb * p sz
[div: ]P la lb / p sz [truncates toward zero]sz
[mod: ]P la lb % p sz [sign matches first operand]sz
[pow: ]P la lb ^ p sz

DCL[edit]

$ inquire a "Enter first number"
$ a = f$integer( a )
$ inquire b "Enter second number"
$ b = f$integer( b )
$ write sys$output "a + b = ", a + b
$ write sys$output "a - b = ", a - b
$ write sys$output "a * b = ", a * b
$ write sys$output "a / b = ", a / b ! truncates down
Output:
$ @arithmetic_integer 
Enter first number: 2
Enter second number: 5
a + b = 7
a - b = -3
a * b = 10
a / b = 0
$ @arithmetic_integer 
Enter first number: -5
Enter second number: -2
a + b = -7
a - b = -3
a * b = 10
a / b = 2

Delphi[edit]

program IntegerArithmetic;
 
{$APPTYPE CONSOLE}
 
uses SysUtils, Math;
 
var
a, b: Integer;
begin
a := StrToInt(ParamStr(1));
b := StrToInt(ParamStr(2));
 
WriteLn(Format('%d + %d = %d', [a, b, a + b]));
WriteLn(Format('%d - %d = %d', [a, b, a - b]));
WriteLn(Format('%d * %d = %d', [a, b, a * b]));
WriteLn(Format('%d / %d = %d', [a, b, a div b])); // rounds towards 0
WriteLn(Format('%d %% %d = %d', [a, b, a mod b])); // matches sign of the first operand
WriteLn(Format('%d ^ %d = %d', [a, b, Trunc(Power(a, b))]));
end.

DWScript[edit]

var a := StrToInt(ParamStr(0));
var b := StrToInt(ParamStr(1));
 
PrintLn(Format('%d + %d = %d', [a, b, a + b]));
PrintLn(Format('%d - %d = %d', [a, b, a - b]));
PrintLn(Format('%d * %d = %d', [a, b, a * b]));
PrintLn(Format('%d / %d = %d', [a, b, a div b]));
PrintLn(Format('%d mod %d = %d', [a, b, a mod b]));
PrintLn(Format('%d ^ %d = %d', [a, b, Trunc(Power(a, b))]));

E[edit]

def arithmetic(a :int, b :int) {
return `$\
Sum: ${a + b}
Difference: ${a - b}
Product: ${a * b}
Quotient: ${a // b}
Remainder: ${a % b}$\n`

}

ECL[edit]

 
ArithmeticDemo(INTEGER A,INTEGER B) := FUNCTION
ADDit  := A + B;
SUBTRACTit  := A - B;
MULTIPLYit  := A * B;
INTDIVIDEit := A DIV B; //INTEGER DIVISION
DIVIDEit  := A / B; //standard division
Remainder  := A % B;
EXPit  := POWER(A,B);
DS  := DATASET([{A,B,'A PLUS B is:',ADDit},
{A,B,'A MINUS B is:',SUBTRACTit},
{A,B,'A TIMES B is:',MULTIPLYit},
{A,B,'A INT DIVIDE BY B is:',INTDIVIDEit},
{A,B,'REMAINDER is:',Remainder},
{A,B,'A DIVIDE BY B is:',DIVIDEit},
{A,B,'A RAISED TO B:',EXPit}],
{INTEGER AVal,INTEGER BVal,STRING18 valuetype,STRING val});
 
RETURN DS;
END;
 
ArithmeticDemo(1,1);
ArithmeticDemo(2,2);
ArithmeticDemo(50,5);
ArithmeticDemo(10,3);
ArithmeticDemo(-1,2);
 
/* NOTE:Division by zero defaults to generating a zero result (0),
rather than reporting a “divide by zero” error.
This avoids invalid or unexpected data aborting a long job.
This default behavior can be changed
*/
 

Efene[edit]

@public
run = fn () {
 
First = io.get_line("First number: ")
Second = io.get_line("Second number: ")
 
A = list_to_integer(lists.delete($\n, First))
B = list_to_integer(lists.delete($\n, Second))
 
io.format("Sum: ~p~n", [A + B])
io.format("Difference: ~p~n", [A - B])
io.format("Product: ~p~n", [A * B])
io.format("Quotient: ~p~n", [A / B])
io.format("Remainder: ~p~n", [A % B])
}

Eiffel[edit]

Works with: SmartEiffel version 2.4

In a file called main.e:

class MAIN
creation make
feature make is
local
a, b: REAL;
do
print("a = ");
io.read_real;
a := io.last_real;
 
print("b = ");
io.read_real;
b := io.last_real;
 
print("a + b = ");
io.put_real(a + b);
print("%Na - b = ");
io.put_real(a - b);
print("%Na * b = ");
io.put_real(a * b);
print("%Na / b = ");
io.put_real(a / b);
print("%Na %% b = ");
io.put_real(((a / b) - (a / b).floor) * b);
print("%Na ^ b = ");
io.put_real(a.pow(b));
print("%N");
end
end

Note that there actually is a builtin modulo operator (\\). However, it seems impossible to use that instruction with SmartEiffel.

Elena[edit]

#define system.
#define system'math.
#define extensions.
 
// --- Program ---
 
#symbol program =
[
#var a := console readLine:(Integer new).
#var b := console readLine:(Integer new).
 
console writeLine:a:" + ": b:" = ":(a + b).
console writeLine:a:" - ": b:" = ":(a - b).
console writeLine:a:" * ": b:" = ":(a * b).
console writeLine:a:" / ": b:" = ":(a / b). // truncates towards 0
console writeLine:a:" % ":b:" = ":(a mod:b). // matches sign of first operand
].

Elixir[edit]

# Function to remove line breaks and convert string to int
get_int = fn msg -> IO.gets(msg) |> String.strip |> String.to_integer end
 
# Get user input
a = get_int.("Enter your first integer: ")
b = get_int.("Enter your second integer: ")
 
IO.puts "Elixir Integer Arithmetic:\n"
IO.puts "Sum: #{a + b}"
IO.puts "Difference: #{a - b}"
IO.puts "Product: #{a * b}"
IO.puts "True Division: #{a / b}" # Float
IO.puts "Division: #{div(a,b)}" # Truncated Towards 0
IO.puts "Remainder: #{rem(a,b)}" # Sign from first digit
IO.puts "Exponent: #{:math.pow(a,b)}" # Float, using Erlang's :math

Erlang[edit]

% Implemented by Arjun Sunel
-module(arith).
-export([start/0]).
 
start() ->
case io:fread("","~d~d") of
{ok, [A,B]} ->
io:format("Sum = ~w~n",[A+B]),
io:format("Difference = ~w~n",[A-B]),
io:format("Product = ~w~n",[A*B]),
io:format("Quotient = ~w~n",[A div B]), % truncates towards zero
io:format("Remainder= ~w~n",[A rem B]), % same sign as the first operand
halt()
end.
 

ERRE[edit]

 
PROGRAM INTEGER_ARITHMETIC
 
!
! for rosettacode.org
!
 
!$INTEGER
 
BEGIN
INPUT("Enter a number ",A)
INPUT("Enter another number ",B)
 
PRINT("Addition ";A;"+";B;"=";(A+B))
PRINT("Subtraction ";A;"-";B;"=";(A-B))
PRINT("Multiplication ";A;"*";B;"=";(A*B))
PRINT("Integer division ";A;"div";B;"=";(A DIV B))
PRINT("Remainder or modulo ";A;"mod";B;"=";(A MOD B))
PRINT("Power ";A;"^";B;"=";(A^B))
END PROGRAM
 
Output:
Enter a number ? 12
Enter another number ? 5
Addition  12 + 5 = 17
Subtraction  12 - 5 = 7
Multiplication  12 * 5 = 60
Integer division  12 div 5 = 2
Remainder or modulo  12 mod 5 = 2
Power  12 ^ 5 = 248832

Truncate towards: 0

Remainder sign matches: first operand

In C-64 ERRE version you must use INT(A/B) for division and A-B*INT(A/B) for modulus.

Euphoria[edit]

include get.e
 
integer a,b
 
a = floor(prompt_number("a = ",{}))
b = floor(prompt_number("b = ",{}))
 
printf(1,"a + b = %d\n", a+b)
printf(1,"a - b = %d\n", a-b)
printf(1,"a * b = %d\n", a*b)
printf(1,"a / b = %g\n", a/b) -- does not truncate
printf(1,"remainder(a,b) = %d\n", remainder(a,b)) -- same sign as first operand
printf(1,"power(a,b) = %g\n", power(a,b))
Output:
a = 2
b = 3
a + b = 5
a - b = -1
a * b = 6
a / b = 0.666667
remainder(a,b) = 2
power(a,b) = 8

Excel[edit]

If the numbers are typed into cells A1 and B1

For sum, type in C1

 
=$A1+$B1
 

For difference, type in D1

 
=$A1-$B1
 

For product, type in E1

 
=$A1*$B1
 

For quotient, type in F1

 
=QUOTIENT($A1,$B1)
 

For remainder, type in G1

 
=MOD($A1,$B1)
 

For exponentiation, type in H1

 
=$A1^$B1
 

Factor[edit]

USING: combinators io kernel math math.functions math.order
math.parser prettyprint ;
 
"a=" "b=" [ write readln string>number ] bi@
{
[ + "sum: " write . ]
[ - "difference: " write . ]
[ * "product: " write . ]
[ / "quotient: " write . ]
[ /i "integer quotient: " write . ]
[ rem "remainder: " write . ]
[ mod "modulo: " write . ]
[ max "maximum: " write . ]
[ min "minimum: " write . ]
[ gcd "gcd: " write . drop ]
[ lcm "lcm: " write . ]
} 2cleave
Output:
a=8
b=12
sum: 20
difference: -4
product: 96
quotient: 2/3
integer quotient: 0
remainder: 8
modulo: 8
maximum: 12
minimum: 8
gcd: 4
lcm: 24

This example illustrates the use of cleave and apply combinators to alleviate the usage of shuffle words in a concatenative language. bi@ applies a quotation to 2 inputs and 2cleave applies a sequence of quotations to 2 inputs.

FALSE[edit]

12 7
\$@$@$@$@$@$@$@$@$@$@\ { 6 copies }
"sum = "+."
difference = "-."
product = "*."
quotient = "/."
modulus = "/*-."
"

Forth[edit]

To keep the example simple, the word takes the two numbers from the stack. /mod returns two results; the stack effect is ( a b -- a%b a/b ).

: arithmetic ( a b -- )
cr ." a=" over . ." b=" dup .
cr ." a+b=" 2dup + .
cr ." a-b=" 2dup - .
cr ." a*b=" 2dup * .
cr ." a/b=" /mod .
cr ." a mod b = " . cr ;

Different host systems have different native signed division behavior. ANS Forth defines two primitive double-precision signed division operations, from which the implementation may choose the most natural to implement the basic divide operations ( / , /mod , mod , */ ). This is partly due to differing specifications in the two previous standards, Forth-79 and Forth-83.

FM/MOD ( d n -- mod div )   \ floored
SM/REM ( d n -- rem div ) \ symmetric
M* ( n n -- d )

In addition, there are unsigned variants.

UM/MOD ( ud u -- umod udiv )
UM* ( u u -- ud )

Fortran[edit]

In ANSI FORTRAN 77 or later:

 INTEGER A, B
PRINT *, 'Type in two integer numbers separated by white space',
+ ' and press ENTER'
READ *, A, B
PRINT *, ' A + B = ', (A + B)
PRINT *, ' A - B = ', (A - B)
PRINT *, ' A * B = ', (A * B)
PRINT *, ' A / B = ', (A / B)
PRINT *, 'MOD(A,B) = ', MOD(A,B)
PRINT *
PRINT *, 'Even though you did not ask, ',
+ 'exponentiation is an intrinsic op in Fortran, so...'
PRINT *, ' A ** B = ', (A ** B)
END

F#[edit]

As F# is a functional language, we can easily create a list of pairs of the string name of a function and the function itself to iterate over printing the operation and applying the function to obtain the result:

 
do
let a, b = int Sys.argv.[1], int Sys.argv.[2]
for str, f in ["+", ( + ); "-", ( - ); "*", ( * ); "/", ( / ); "%", ( % )] do
printf "%d %s %d = %d\n" a str b (f a b)
 

For example, the output with the arguments 4 and 3 is:

 
4 + 3 = 7
4 - 3 = 1
4 * 3 = 12
4 / 3 = 1
4 % 3 = 1
 


friendly interactive shell[edit]

 
read a
read b
echo 'a + b =' (math "$a + $b") # Sum
echo 'a - b =' (math "$a - $b") # Difference
echo 'a * b =' (math "$a * $b") # Product
echo 'a / b =' (math "$a / $b") # Integer quotient
echo 'a % b =' (math "$a % $b") # Remainder
echo 'a ^ b =' (math "$a ^ $b") # Exponentation
 

Frink[edit]

This demonstrates normal division (which produces rational numbers when possible), div, and mod. div rounds toward negative infinity (defined as floor[x/y]). mod uses the sign of the second number (defined as x - y * floor[x/y]). All operators automatically produce big integers or exact rational numbers when necessary.

 
[a,b] = input["Enter numbers",["a","b"]]
ops=["+", "-", "*", "/", "div" ,"mod" ,"^"]
for op = ops
{
str = "$a $op $b"
println["$str = " + eval[str]]
}
 
Output:
 
10 + 20 = 30
10 - 20 = -10
10 * 20 = 200
10 / 20 = 1/2 (exactly 0.5)
10 div 20 = 0
10 mod 20 = 10
10 ^ 20 = 100000000000000000000
 

GAP[edit]

run := function()
local a, b, f;
f := InputTextUser();
Print("a =\n");
a := Int(Chomp(ReadLine(f)));
Print("b =\n");
b := Int(Chomp(ReadLine(f)));
Display(Concatenation(String(a), " + ", String(b), " = ", String(a + b)));
Display(Concatenation(String(a), " - ", String(b), " = ", String(a - b)));
Display(Concatenation(String(a), " * ", String(b), " = ", String(a * b)));
Display(Concatenation(String(a), " / ", String(b), " = ", String(QuoInt(a, b)))); # toward 0
Display(Concatenation(String(a), " mod ", String(b), " = ", String(RemInt(a, b)))); # nonnegative
Display(Concatenation(String(a), " ^ ", String(b), " = ", String(a ^ b)));
CloseStream(f);
end;

GEORGE[edit]

R (m) ;
R (n) ;
m n + P;
m n - P;
m n × P;
m n div P;
m n rem P;

Go[edit]

int[edit]

package main
 
import "fmt"
 
func main() {
var a, b int
fmt.Print("enter two integers: ")
fmt.Scanln(&a, &b)
fmt.Printf("%d + %d = %d\n", a, b, a+b)
fmt.Printf("%d - %d = %d\n", a, b, a-b)
fmt.Printf("%d * %d = %d\n", a, b, a*b)
fmt.Printf("%d / %d = %d\n", a, b, a/b) // truncates towards 0
fmt.Printf("%d %% %d = %d\n", a, b, a%b) // same sign as first operand
// no exponentiation operator
}
Example run:
enter two integers: -5 3
-5 + 3 = -2
-5 - 3 = -8
-5 * 3 = -15
-5 / 3 = -1
-5 % 3 = -2

big.Int[edit]

package main
 
import (
"fmt"
"math/big"
)
 
func main() {
var a, b, c big.Int
fmt.Print("enter two integers: ")
fmt.Scan(&a, &b)
fmt.Printf("%d + %d = %d\n", &a, &b, c.Add(&a, &b))
fmt.Printf("%d - %d = %d\n", &a, &b, c.Sub(&a, &b))
fmt.Printf("%d * %d = %d\n", &a, &b, c.Mul(&a, &b))
 
// Quo, Rem functions work like Go operators on int:
// quo truncates toward 0,
// and a non-zero rem has the same sign as the first operand.
fmt.Printf("%d quo %d = %d\n", &a, &b, c.Quo(&a, &b))
fmt.Printf("%d rem %d = %d\n", &a, &b, c.Rem(&a, &b))
 
// Div, Mod functions do Euclidean division:
// the result m = a mod b is always non-negative,
// and for d = a div b, the results d and m give d*y + m = x.
fmt.Printf("%d div %d = %d\n", &a, &b, c.Div(&a, &b))
fmt.Printf("%d mod %d = %d\n", &a, &b, c.Mod(&a, &b))
 
// as with int, no exponentiation operator
}
Example run:
enter two integers: -5 3
-5 + 3 = -2
-5 - 3 = -8
-5 * 3 = -15
-5 quo 3 = -1
-5 rem 3 = -2
-5 div 3 = -2
-5 mod 3 = 1

Groovy[edit]

def arithmetic = { a, b ->
println """
a + b = ${a} + ${b} = ${a + b}
a - b = ${a} - ${b} = ${a - b}
a * b = ${a} * ${b} = ${a * b}
a / b = ${a} / ${b} = ${a / b}  !!! Converts to floating point!
(int)(a / b) = (int)(${a} / ${b}) = ${(int)(a / b)}  !!! Truncates downward after the fact
a.intdiv(b) = ${a}.intdiv(${b}) = ${a.intdiv(b)}  !!! Behaves as if truncating downward, actual implementation varies
a % b = ${a} % ${b} = ${a % b}
 
Exponentiation is also a base arithmetic operation in Groovy, so:
a ** b = ${a} ** ${b} = ${a ** b}
"""

}

Program:

arithmetic(5,3)
Output:
       a + b =        5 + 3 = 8
       a - b =        5 - 3 = 2
       a * b =        5 * 3 = 15
       a / b =        5 / 3 = 1.6666666667   !!! Converts to floating point!
(int)(a / b) = (int)(5 / 3) = 1              !!! Truncates downward after the fact
 a.intdiv(b) =  5.intdiv(3) = 1              !!! Behaves as if truncating downward, actual implementation varies
       a % b =        5 % 3 = 2

Exponentiation is also a base arithmetic operation in Groovy, so:
      a ** b =       5 ** 3 = 125

Harbour[edit]

procedure Test( a, b )
? "a+b", a + b
? "a-b", a - b
? "a*b", a * b
// The quotient isn't integer, so we use the Int() function, which truncates it downward.
? "a/b", Int( a / b )
// Remainder:
? "a%b", a % b
// Exponentiation is also a base arithmetic operation
? "a**b", a ** b
return

Haskell[edit]

main = do
a <- readLn :: IO Integer
b <- readLn :: IO Integer
putStrLn $ "a + b = " ++ show (a + b)
putStrLn $ "a - b = " ++ show (a - b)
putStrLn $ "a * b = " ++ show (a * b)
putStrLn $ "a to the power of b = " ++ show (a ** b)
putStrLn $ "a to the power of b = " ++ show (a ^ b)
putStrLn $ "a to the power of b = " ++ show (a ^^ b)
putStrLn $ "a `div` b = " ++ show (a `div` b) -- truncates towards negative infinity
putStrLn $ "a `mod` b = " ++ show (a `mod` b) -- same sign as second operand
putStrLn $ "a `divMod` b = " ++ show (a `divMod` b)
putStrLn $ "a `quot` b = " ++ show (a `quot` b) -- truncates towards 0
putStrLn $ "a `rem` b = " ++ show (a `rem` b) -- same sign as first operand
putStrLn $ "a `quotRem` b = " ++ show (a `quotRem` b)

Haxe[edit]

class BasicIntegerArithmetic {
public static function main() {
var args =Sys.args();
if (args.length < 2) return;
var a = Std.parseFloat(args[0]);
var b = Std.parseFloat(args[1]);
trace("a+b = " + (a+b));
trace("a-b = " + (a-b));
trace("a*b = " + (a*b));
trace("a/b = " + (a/b));
trace("a%b = " + (a%b));
}
}

HicEst[edit]

All numeric is 8-byte-float. Conversions are by INT, NINT, FLOOR, CEILING, or Formatted IO

DLG(Edit=A, Edit=B, TItle='Enter numeric A and B')
WRITE(Name) A, B
WRITE() ' A + B = ', A + B
WRITE() ' A - B = ', A - B
WRITE() ' A * B = ', A * B
WRITE() ' A / B = ', A / B ! no truncation
WRITE() 'truncate A / B = ', INT(A / B) ! truncates towards 0
WRITE() 'round next A / B = ', NINT(A / B) ! truncates towards next integer
WRITE() 'round down A / B = ', FLOOR(A / B) ! truncates towards minus infinity
WRITE() 'round up A / B = ', CEILING(A / B) ! truncates towards plus infinity
WRITE() 'remainder of A / B = ', MOD(A, B) ! same sign as A
WRITE() 'A to the power of B = ', A ^ B
WRITE() 'A to the power of B = ', A ** B
A=5; B=-4;
A + B = 1
A - B = 9
A * B = -20
A / B = -1.25
truncate A / B = -1
round next A / B = -1
round down A / B = -2
round up A / B = -1
remainder of A / B = 1
A to the power of B = 16E-4
A to the power of B = 16E-4

Icon and Unicon[edit]

procedure main()
writes("Input 1st integer a := ")
a := integer(read())
writes("Input 2nd integer b := ")
b := integer(read())
 
write(" a + b = ",a+b)
write(" a - b = ",a-b)
write(" a * b = ",a*b)
write(" a / b = ",a/b, " rounds toward 0")
write(" a % b = ",a%b, " remainder sign matches a")
write(" a ^ b = ",a^b)
end

Inform 7[edit]

Enter Two Numbers is a room.
 
Numerically entering is an action applying to one number. Understand "[number]" as numerically entering.
 
The first number is a number that varies.
 
After numerically entering for the first time:
now the first number is the number understood.
 
After numerically entering for the second time:
let A be the first number;
let B be the number understood;
say "[A] + [B] = [A + B]."; [operator syntax]
say "[A] - [B] = [A minus B]."; [English syntax]
let P be given by P = A * B where P is a number; [inline equation]
say "[A] * [B] = [P].";
let Q be given by the Division Formula; [named equation]
say "[A] / [B] = [Q].";
say "[A] mod [B] = [remainder after dividing A by B].";
end the story.
 
Equation - Division Formula
Q = A / B
where Q is a number, A is a number, and B is a number.

This solution shows four syntaxes: mathematical operators, English operators, inline equations, and named equations. Division rounds toward zero, and the remainder has the same sign as the quotient.

J[edit]

calc =:    + , - , * , <.@% , |~ , ^

The function calc constructs a list of numeric results for this task. The implementation of integer division we use here (<.@%.) rounds down (towards negative infinity), and this is compatible with the remainder implementation we use here.

   17 calc 3
20 14 51 5 2 4913

The function bia assembles these results, textually:

labels  =: ];.2 'Sum: Difference: Product: Quotient: Remainder: Exponentiation: '
combine =: ,. ":@,.
bia =: labels combine calc
 
17 bia 3
Sum: 20
Difference: 14
Product: 51
Quotient: 5
Remainder: 2
Exponentiation: 4913

Java[edit]

import java.util.Scanner;
public class Int{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
 
int sum = a + b;//integer addition is discouraged in print statements due to confusion with String concatenation
System.out.println("a + b = " + sum);
System.out.println("a - b = " + (a - b));
System.out.println("a * b = " + (a * b));
System.out.println("quotient of a / b = " + (a / b)); // truncates towards 0
System.out.println("remainder of a / b = " + (a % b)); // same sign as first operand
}
}

JavaScript[edit]

Works with: JScript
Works with: SpiderMonkey

Note that the operators work the same in all versions of JavaScript; the requirement for specific implementations is in order to get user input.

var a = parseInt(get_input("Enter an integer"), 10);
var b = parseInt(get_input("Enter an integer"), 10);
 
WScript.Echo("a = " + a);
WScript.Echo("b = " + b);
WScript.Echo("sum: a + b = " + (a + b));
WScript.Echo("difference: a - b = " + (a - b));
WScript.Echo("product: a * b = " + (a * b));
WScript.Echo("quotient: a / b = " + (a / b | 0)); // "| 0" casts it to an integer
WScript.Echo("remainder: a % b = " + (a % b));
 
function get_input(prompt) {
output(prompt);
try {
return WScript.StdIn.readLine();
} catch(e) {
return readline();
}
}
function output(prompt) {
try {
WScript.Echo(prompt);
} catch(e) {
print(prompt);
}
}
Output:
Enter an integer
-147
Enter an integer
63
a = -147
b = 63
sum: a + b = -84
difference: a - b = -210
product: a * b = -9261
quotient: a / b = -2
remainder: a % b = -21

jq[edit]

# Lines which do not have two integers are skipped:
 
def arithmetic:
split(" ") | select(length > 0) | map(tonumber)
| if length > 1 then
.[0] as $a | .[1] as $b
| "For a = \($a) and b = \($b):\n" +
"a + b = \($a + $b)\n" +
"a - b = \($a - $b)\n" +
"a * b = \($a * $b)\n" +
"a/b|floor = \($a / $b | floor)\n" +
"a % b = \($a % $b)\n" +
"a | exp = \($a | exp)\n"
else empty
end ;
 
arithmetic
 
Output:
$ jq -R -r -f arithmetic.jq
7 -2
For a = 7 and b = -2:
a + b = 5
a - b = 9
a * b = -14
a/b|floor = -4
a % b = 1
a | exp = 1096.6331584284585

2 -7
For a = 2 and b = -7:
a + b = -5
a - b = 9
a * b = -14
a/b|floor = -1
a % b = 2
a | exp = 7.38905609893065

-2 -7
For a = -2 and b = -7:
a + b = -9
a - b = 5
a * b = 14
a/b|floor = 0
a % b = -2
a | exp = 0.1353352832366127

Julia[edit]

function arithmetic (a = int(readline()), b = int(readline()))
for op in [+,-,*,div,rem]
println("a $op b = $(op(a,b))")
end
end
Output:
julia> arithmetic()
4
5
a + b = 9
a - b = -1
a * b = 20
a div b = 0
a rem b = 4

LabVIEW[edit]

This image is a VI Snippet, an executable image of LabVIEW code. The LabVIEW version is shown on the top-right hand corner. You can download it, then drag-and-drop it onto the LabVIEW block diagram from a file browser, and it will appear as runnable, editable code.
LabVIEW Arithmetic Integer.png


Lasso[edit]

local(a = 6, b = 4)
#a + #b // 10
#a - #b // 2
#a * #b // 24
#a / #b // 1
#a % #b // 2
math_pow(#a,#b) // 1296
math_pow(#b,#a) // 4096

LFE[edit]

 
(defmodule arith
(export all))
 
(defun demo-arith ()
(case (: io fread '"Please enter two integers: " '"~d~d")
((tuple 'ok (a b))
(: io format '"~p + ~p = ~p~n" (list a b (+ a b)))
(: io format '"~p - ~p = ~p~n" (list a b (- a b)))
(: io format '"~p * ~p = ~p~n" (list a b (* a b)))
(: io format '"~p^~p = ~p~n" (list a b (: math pow a b)))
; div truncates towards zero
(: io format '"~p div ~p = ~p~n" (list a b (div a b)))
; rem's result takes the same sign as the first operand
(: io format '"~p rem ~p = ~p~n" (list a b (rem a b))))))
 

Usage from the LFE REPL:

 
> (slurp '"arith.lfe")
#(ok arith)
> (demo-arith)
Please enter two integers: 2 8
2 + 8 = 10
2 - 8 = -6
2 * 8 = 16
2^8 = 256.0
2 div 8 = 0
2 rem 8 = 2
ok
 

Liberty BASIC[edit]

Note that raising to a power can display very large integers without going to approximate power-of-ten notation.

 
input "Enter the first integer: "; first
input "Enter the second integer: "; second
 
print "The sum is " ; first + second
print "The difference is " ; first -second
print "The product is " ; first *second
if second <>0 then print "The integer quotient is " ; int( first /second); " (rounds towards 0)" else print "Division by zero not allowed."
print "The remainder is " ; first MOD second; " (sign matches first operand)"
print "The first raised to the power of the second is " ; first ^second
 

Little[edit]

# Maybe you need to import the mathematical funcions 
# from Tcl with:
# eval("namespace path ::tcl::mathfunc");
 
void main() {
int a, b;
puts("Enter two integers:");
a = (int)(gets(stdin));
b = (int)(gets(stdin));
puts("${a} + ${b} = ${a+b}");
puts("${a} - ${b} = ${a-b}");
puts("${a} * ${b} = ${a*b}");
puts("${a} / ${b} = ${a/b}, remainder ${a%b}");
puts("${a} to the power of ${b} = ${(int)pow(a,b)}");
}

LiveCode[edit]

ask "enter 2 numbers (comma separated)"
if it is not empty then
put item 1 of it into n1
put item 2 of it into n2
put sum(n1,n2) into ai["sum"]
put n1 * n2 into ai["product"]
put n1 div n2 into ai["quotient"] -- truncates
put n1 mod n2 into ai["remainder"]
put n1^n2 into ai["power"]
combine ai using comma and colon
put ai
end if
Examples
-2,4  - power:16,product:-8,quotient:0,remainder:-2,sum:2
2,-4 - power:0.0625,product:-8,quotient:0,remainder:2,sum:-2
-2,-4 - power:0.0625,product:8,quotient:0,remainder:-2,sum:-6
2,4 - power:16,product:8,quotient:0,remainder:2,sum:6
11,4 - power:14641,product:44,quotient:2,remainder:3,sum:15

[edit]

to operate :a :b
(print [a =] :a)
(print [b =] :b)
(print [a + b =] :a + :b)
(print [a - b =] :a - :b)
(print [a * b =] :a * :b)
(print [a / b =] int :a / :b)
(print [a mod b =] modulo :a :b)
end

Each infix operator also has a prefix synonym (sum, difference, product, quotient). Sum and product can also have arity greater than two when used in parentheses (sum 1 2 3). Infix operators in general have high precedence; you may need to enclose their arguments in parentheses to obtain the correct expression.

LSE64[edit]

over : 2 pick
2dup : over over
 
arithmetic : \
" A=" ,t over , sp " B=" ,t dup , nl \
" A+B=" ,t 2dup + , nl \
" A-B=" ,t 2dup - , nl \
" A*B=" ,t 2dup * , nl \
" A/B=" ,t 2dup / , nl \
" A%B=" ,t  % , nl

Lua[edit]

local x = io.read()
local y = io.read()
 
print ("Sum: " , (x + y))
print ("Difference: ", (x - y))
print ("Product: " , (x * y))
print ("Quotient: " , (x / y)) -- Does not truncate
print ("Remainder: " , (x % y)) -- Result has sign of right operand
print ("Exponent: " , (x ^ y))

M4[edit]

Because of the particular nature of M4, the only user-input is the code itself. Anyway the following code can be used:

eval(A+B)
eval(A-B)
eval(A*B)
eval(A/B)
eval(A%B)

once saved in a file, e.g. operations.m4:

m4 -DA=4 -DB=6 operations.m4

or using a sort of driver:

define(`A', 4)dnl
define(`B', 6)dnl
include(`operations.m4')

Maple[edit]

These operations are all built-in. As all operations are exact, there are no rounding issues involved.

 
DoIt := proc()
local a := readstat( "Input an integer: " ):
local b := readstat( "Input another integer: " ):
printf( "Sum = %d\n", a + b ):
printf( "Difference = %d\n", a - b ):
printf( "Product = %d\n", a * b ):
printf( "Quotient = %d\n", iquo( a, b, 'c' ) ):
printf( "Remainder = %d\n", c ); # or irem( a, b )
NULL # quiet return
end proc:
 

Here is an example of calling DoIt.

 
> DoIt();
Input an integer: 15;
Input another integer: 12;
Sum = 27
Difference = 3
Product = 180
Quotient = 1
Remainder = 3
>
 

Mathematica[edit]

Mathematica has all the function built-in to handle this task. Example:

a = Input["Give me an integer please!"];
b = Input["Give me another integer please!"];
Print["You gave me ", a, " and ", b];
Print["sum: ", a + b];
Print["difference: ", a - b];
Print["product: ", a b];
Print["integer quotient: ", IntegerPart[a/b]];
Print["remainder: ", Mod[a, b]];
Print["exponentiation: ", a^b];

gives back for input 17 and 3: <preMathematica>You gave me 17 and 3 sum: 20 difference: 14 product: 51 integer quotient: 5 remainder: 2 exponentiation: 4913</pre>

MATLAB / Octave[edit]

disp("integer a: "); a = scanf("%d", 1);
disp("integer b: "); b = scanf("%d", 1);
a+b
a-b
a*b
floor(a/b)
mod(a,b)
a^b

Maxima[edit]

block(
[a: read("a"), b: read("b")],
print(a + b),
print(a - b),
print(a * b),
print(a / b),
print(quotient(a, b)),
print(remainder(a, b)),
a^b
);

MAXScript[edit]

x = getKBValue prompt:"First number"
y = getKBValue prompt:"Second number:"
 
format "Sum: %\n" (x + y)
format "Difference: %\n" (x - y)
format "Product: %\n" (x * y)
format "Quotient: %\n" (x / y)
format "Remainder: %\n" (mod x y)

Mercury[edit]

 
:- module arith_int.
:- interface.
 
:- import_module io.
:- pred main(io::di, io::uo) is det.
 
:- implementation.
:- import_module int, list, string.
 
main(!IO) :-
io.command_line_arguments(Args, !IO),
( if
Args = [AStr, BStr],
string.to_int(AStr, A),
string.to_int(BStr, B)
then
io.format("A + B = %d\n", [i(A + B)], !IO),
io.format("A - B = %d\n", [i(A - B)], !IO),
io.format("A * B = %d\n", [i(A * B)], !IO),
 
 % Division: round towards zero.
 %
io.format("A / B = %d\n", [i(A / B)], !IO),
 
 % Division: round towards minus infinity.
 %
io.format("A div B = %d\n", [i(A div B)], !IO),
 
 % Modulus: X mod Y = X - (X div Y) * Y.
 %
io.format("A mod B = %d\n", [i(A mod B)], !IO),
 
 % Remainder: X rem Y = X - (X / Y) * Y.
 %
io.format("A rem B = %d\n", [i(A rem B)], !IO),
 
 % Exponentiation is done using the function int.pow/2.
 %
io.format("A `pow` B = %d\n", [i(A `pow` B)], !IO)
else
io.set_exit_status(1, !IO)
).
 

Metafont[edit]

string s[];
message "input number a: ";
s1 := readstring;
message "input number b: ";
s2 := readstring;
a := scantokens s1;
b := scantokens s2;
 
def outp(expr op) =
message "a " & op & " b = " & decimal(a scantokens(op) b) enddef;
 
outp("+");
outp("-");
outp("*");
outp("div");
outp("mod");
 
end

МК-61/52[edit]

П1	<->	П0
+ С/П
ИП0 ИП1 - С/П
ИП0 ИП1 * С/П
ИП0 ИП1 / [x] С/П
ИП0 ^ ИП1 / [x] ИП1 * - С/П
ИП1 ИП0 x^y С/П

ML/I[edit]

ML/I will read two integers from 'standard input' or similar, and then output the results to 'standard output' or similar.

MCSKIP "WITH" NL
"" Arithmetic/Integer
"" assumes macros on input stream 1, terminal on stream 2
MCSKIP MT,<>
MCINS %.
MCDEF SL SPACES NL AS <MCSET T1=%A1.
MCSET T2=%A2.
a + b = %%T1.+%T2..
a - b = %%T1.-%T2..
a * b = %%T1.*%T2..
a / b = %%T1./%T2..
a rem b = %%T1.-%%%T1./%T2..*%T2...
Division is truncated to the greatest integer
that does not exceed the exact result. Remainder matches
the sign of the second operand, if the signs differ.

Modula-2[edit]

MODULE ints;
 
IMPORT InOut;
 
VAR a, b : INTEGER;
 
BEGIN
InOut.WriteString ("Enter two integer numbers : "); InOut.WriteBf;
InOut.ReadInt (a);
InOut.ReadInt (b);
InOut.WriteString ("a + b = "); InOut.WriteInt (a + b, 9); InOut.WriteLn;
InOut.WriteString ("a - b = "); InOut.WriteInt (a - b, 9); InOut.WriteLn;
InOut.WriteString ("a * b = "); InOut.WriteInt (a * b, 9); InOut.WriteLn;
InOut.WriteString ("a / b = "); InOut.WriteInt (a DIV b, 9); InOut.WriteLn;
InOut.WriteString ("a MOD b = "); InOut.WriteInt (a MOD b, 9); InOut.WriteLn;
InOut.WriteLn;
END ints.
Producing:
$$ ints

Enter two integer numbers : 12 7 a + b = 19 a - b = 5 a * b = 84 a / b = 1 a MOD b = 5

$$ ints Enter two integer numbers : 123 -111 a + b = 12 a - b = 234 a * b = -13653 a / b = -1

a MOD b = 12

Modula-3[edit]

MODULE Arith EXPORTS Main;
 
IMPORT IO, Fmt;
 
VAR a, b: INTEGER;
 
BEGIN
a := IO.GetInt();
b := IO.GetInt();
IO.Put("a+b = " & Fmt.Int(a + b) & "\n");
IO.Put("a-b = " & Fmt.Int(a - b) & "\n");
IO.Put("a*b = " & Fmt.Int(a * b) & "\n");
IO.Put("a DIV b = " & Fmt.Int(a DIV b) & "\n");
IO.Put("a MOD b = " & Fmt.Int(a MOD b) & "\n");
END Arith.

MUMPS[edit]

Note: M[UMPS] has an operator called "modulo". When both operands are positive numbers, "modulo" has a result that looks a lot like "remainder"; however, there is an important difference.

To better understand the intricacies of "modulo" and how it is different from "remainder", see Donald Knuth's definition (Volume 1 of the "big books"), or find out the beauty of cyclic algebra as formulated by Niels Henrik Abel (August 5, 1802 – April 6, 1829).

Arith(first,second)	; Mathematical operators
Write "Plus",?12,first,"+",second,?25," = ",first+second,!
Write "Minus",?12,first,"-",second,?25," = ",first-second,!
Write "Multiply",?12,first,"*",second,?25," = ",first*second,!
Write "Divide",?12,first,"/",second,?25," = ",first/second,!
Write "Int Divide",?12,first,"\",second,?25," = ",first\second,!
Write "Power",?12,first,"**",second,?25," = ",first**second,!
Write "Modulo",?12,first,"#",second,?25," = ",first#second,!
Write "And",?12,first,"&",second,?25," = ",first&second,!
Write "Or",?12,first,"!",second,?25," = ",first!second,!
Quit
 
Do Arith(2,3)
Plus 2+3 = 5
Minus 2-3 = -1
Multiply 2*3 = 6
Divide 2/3 = .6666666666666666667
Int Divide 2\3 = 0
Power 2**3 = 8
Modulo 2#3 = 2
And 2&3 = 1
Or 2!3 = 1
 
Do Arith(16,0.5)
Plus 16+.5 = 16.5
Minus 16-.5 = 15.5
Multiply 16*.5 = 8
Divide 16/.5 = 32
Int Divide 16\.5 = 32
Power 16**.5 = 4
Modulo 16#.5 = 0
And 16&.5 = 1
Or 16!.5 = 1
 
Do Arith(0,2)
Plus 0+2 = 2
Minus 0-2 = -2
Multiply 0*2 = 0
Divide 0/2 = 0
Int Divide 0\2 = 0
Power 0**2 = 0
Modulo 0#2 = 0
And 0&2 = 0
Or 0!2 = 1


Nemerle[edit]

Adapted nearly verbatim from C# solution above. Note that I've used the exponentiation operator (**), but Math.Pow() as used in the C# solution would also work.

using System;
 
class Program
{
static Main(args : array[string]) : void
{
def a = Convert.ToInt32(args[0]);
def b = Convert.ToInt32(args[1]);
 
Console.WriteLine("{0} + {1} = {2}", a, b, a + b);
Console.WriteLine("{0} - {1} = {2}", a, b, a - b);
Console.WriteLine("{0} * {1} = {2}", a, b, a * b);
Console.WriteLine("{0} / {1} = {2}", a, b, a / b); // truncates towards 0
Console.WriteLine("{0} % {1} = {2}", a, b, a % b); // matches sign of first operand
Console.WriteLine("{0} ** {1} = {2}", a, b, a ** b);
}
}

NetRexx[edit]

Translation of: REXX
/* NetRexx */
 
options replace format comments java crossref symbols binary
 
say "enter 2 integer values separated by blanks"
parse ask a b
say a "+" b "=" a + b
say a "-" b "=" a - b
say a "*" b "=" a * b
say a "/" b "=" a % b "remaining" a // b "(sign from first operand)"
say a "^" b "=" a ** b
 
return
 
Output:
enter 2 integer values separated by blanks
17 -4
17 + -4 = 13
17 - -4 = 21
17 * -4 = -68
17 / -4 = -4 remaining 1 (sign from first operand)
17 ^ -4 = 0.0000119730367

NewLISP[edit]

; integer.lsp
; oofoe 2012-01-17
 
(define (aski msg) (print msg) (int (read-line)))
(setq x (aski "Please type in an integer and press [enter]: "))
(setq y (aski "Please type in another integer  : "))
 
; Note that +, -, *, / and % are all integer operations.
(println)
(println "Sum: " (+ x y))
(println "Difference: " (- x y))
(println "Product: " (* x y))
(println "Integer quotient (rounds to 0): " (/ x y))
(println "Remainder: " (setq r (% x y)))
 
(println "Remainder sign matches: "
(cond ((= (sgn r) (sgn x) (sgn y)) "both")
((= (sgn r) (sgn x)) "first")
((= (sgn r) (sgn y)) "second")))
 
(println)
(println "Exponentiation: " (pow x y))
 
(exit) ; NewLisp normally goes to listener after running script.
 
Output:
Please type in an integer and press [enter]: 17
Please type in another integer             : -4

Sum: 13
Difference: 21
Product: -68
Integer quotient (rounds to 0): -4
Remainder: 1
Remainder sign matches: first

Exponentiation: 1.197303672e-005

Nim[edit]

 
import parseopt,strutils
 
var
opt: TOptParser = initOptParser()
str = opt.cmdLineRest.split
a: int = 0
b: int = 0
 
try:
a = parseInt(str[0])
b = parseInt(str[1])
except EinvalidValue:
quit("Invalid params. Two integers are expected.")
 
 
echo ("a  : " & $a)
echo ("b  : " & $b)
echo ("a + b  : " & $(a+b))
echo ("a - b  : " & $(a-b))
echo ("a * b  : " & $(a*b))
echo ("a div b: " & $(a div b))
echo ("a mod b: " & $(a mod b))
 

Execute: Aritmint 10 23
/

Output:
a      : 10
b      : 23
a + b  : 33
a - b  : -13
a * b  : 230
a div b: 0
a mod b: 10

NSIS[edit]

All Arithmetic in NSIS is handled by the IntOp instruction. It is beyond the scope of this task to implement user input (a fairly involved task), so I will be providing hard-coded values simulating the user input, with the intention of later adding the user-input piece.

Function Arithmetic
Push $0
Push $1
Push $2
StrCpy $0 21
StrCpy $1 -2
 
IntOp $2 $0 + $1
DetailPrint "$0 + $1 = $2"
IntOp $2 $0 - $1
DetailPrint "$0 - $1 = $2"
IntOp $2 $0 * $1
DetailPrint "$0 * $1 = $2"
IntOp $2 $0 / $1
DetailPrint "$0 / $1 = $2"
DetailPrint "Rounding is toward negative infinity"
IntOp $2 $0 % $1
DetailPrint "$0 % $1 = $2"
DetailPrint "Sign of remainder matches the first number"
 
Pop $2
Pop $1
Pop $0
FunctionEnd

Oberon-2[edit]

Oxford Oberon-2

 
MODULE Arithmetic;
IMPORT In, Out;
VAR
x,y:INTEGER;
BEGIN
Out.String("Give two numbers: ");In.Int(x);In.Int(y);
Out.String("x + y >");Out.Int(x + y,6);Out.Ln;
Out.String("x - y >");Out.Int(x - y,6);Out.Ln;
Out.String("x * y >");Out.Int(x * y,6);Out.Ln;
Out.String("x / y >");Out.Int(x DIV y,6);Out.Ln;
Out.String("x MOD y >");Out.Int(x MOD y,6);Out.Ln;
END Arithmetic.
 
Output:
Give two numbers: 12 23
x + y >    35
x - y >   -11
x * y >   276
x / y >     0
x MOD y >    12

Objeck[edit]

bundle Default {
class Arithmetic {
function : Main(args : System.String[]) ~ Nil {
DoArithmetic();
}
 
function : native : DoArithmetic() ~ Nil {
a := IO.Console->GetInstance()->ReadString()->ToInt();
b := IO.Console->GetInstance()->ReadString()->ToInt();
 
IO.Console->GetInstance()->Print("a+b = ")->PrintLine(a+b);
IO.Console->GetInstance()->Print("a-b = ")->PrintLine(a-b);
IO.Console->GetInstance()->Print("a*b = ")->PrintLine(a*b);
IO.Console->GetInstance()->Print("a/b = ")->PrintLine(a/b);
}
}
}

OCaml[edit]

let _ =
let a = read_int ()
and b = read_int () in
 
Printf.printf "a + b = %d\n" (a + b);
Printf.printf "a - b = %d\n" (a - b);
Printf.printf "a * b = %d\n" (a * b);
Printf.printf "a / b = %d\n" (a / b); (* truncates towards 0 *)
Printf.printf "a mod b = %d\n" (a mod b) (* same sign as first operand *)

Oforth[edit]

: integers(a, b)
"a + b =" . a b + .cr
"a - b =" . a b - .cr
"a * b =" . a b * .cr
"a / b =" . a b / .cr
"a mod b =" . a b mod .cr
"a pow b =" . a b pow .cr ;
Output:
>12 23 integers
a + b = 35
a - b = -11
a * b = 276
a / b = 0
a mod b = 12
a pow b = 6624737266949237011120128
ok

Onyx[edit]

# Most of this long script is mere presentation.
# All you really need to do is push two integers onto the stack
# and then execute add, sub, mul, idiv, or pow.
 
$ClearScreen { # Using ANSI terminal control
`\e[2J\e[1;1H' print flush
} bind def
 
$Say { # string Say -
`\n' cat print flush
} bind def
 
$ShowPreamble {
`To show how integer arithmetic in done in Onyx,' Say
`we\'ll use two numbers of your choice, which' Say
`we\'ll call A and B.\n' Say
} bind def
 
$Prompt { # stack: string --
stdout exch write pop flush
} def
 
$GetInt { # stack: name -- integer
dup cvs `Enter integer ' exch cat `: ' cat
Prompt stdin readline pop cvx eval def
} bind def
 
$Template { # arithmetic_operator_name label_string Template result_string
A cvs ` ' B cvs ` ' 5 ncat over cvs ` gives ' 3 ncat exch
A B dn cvx eval cvs `.' 3 ncat Say
} bind def
 
$ShowResults {
$add `Addition: ' Template
$sub `Subtraction: ' Template
$mul `Multiplication: ' Template
$idiv `Division: ' Template
`Note that the result of integer division is rounded toward zero.' Say
$pow `Exponentiation: ' Template
`Note that the result of raising to a negative power always gives a real number.' Say
} bind def
 
ClearScreen ShowPreamble $A GetInt $B GetInt ShowResults
Output:
To show how integer arithmetic in done in Onyx,
we'll use two numbers of your choice, which
we'll call A and B.

Enter integer A: 34
Enter integer B: 2
Addition: 34 2 add gives 36.
Subtraction: 34 2 sub gives 32.
Multiplication: 34 2 mul gives 68.
Division: 34 2 idiv gives 17.
Note that the result of integer division is rounded toward zero.
Exponentiation: 34 2 pow gives 1156.
Note that the result of raising to a negative power always gives a real number.

Openscad[edit]

echo (a+b);  /* Sum */
echo (a-b); /* Difference */
echo (a*b); /* Product */
echo (a/b); /* Quotient */
echo (a%b); /* Modulus */

Oz[edit]

declare
StdIn = {New class $ from Open.file Open.text end init(name:stdin)}
 
fun {ReadInt}
{String.toInt {StdIn getS($)}}
end
 
A = {ReadInt}
B = {ReadInt}
in
{ForAll
["A+B = "#A+B
"A-B = "#A-B
"A*B = "#A*B
"A/B = "#A div B %% truncates towards 0
"remainder "#A mod B %% has the same sign as A
"A^B = "#{Pow A B}
]
System.showInfo}

PARI/GP[edit]

Integer division with \ rounds to . There also exists the \/ round-to-nearest (ties to ) operator. Ordinary division / does not round but returns rationals if given integers with a non-integral quotient.

arith(a,b)={
print(a+b);
print(a-b);
print(a*b);
print(a\b);
print(a%b);
print(a^b);
};

Panda[edit]

Use reflection to get all functions defined on numbers taking number and returning number.

a=3 b=7 func:_bbf__number_number_number =>f.name.<b> '(' a b ')' ' => ' f(a b) nl
Output:
atan2 ( 3 7 ) => 0.40489178628508343 
divide ( 3 7 ) => 0.42857142857142855 
gt ( 3 7 ) => UNDEFINED! 
gte ( 3 7 ) => UNDEFINED! 
lt ( 3 7 ) => 3 
lte ( 3 7 ) => 3 
max ( 3 7 ) => 7 
min ( 3 7 ) => 3 
minus ( 3 7 ) => -4 
mod ( 3 7 ) => 3 
plus ( 3 7 ) => 10 
pow ( 3 7 ) => 2187

Pascal[edit]

program arithmetic(input, output)
 
var
a, b: integer;
 
begin
readln(a, b);
writeln('a+b = ', a+b);
writeln('a-b = ', a-b);
writeln('a*b = ', a*b);
writeln('a/b = ', a div b, ', remainder ', a mod b);
end.

Perl[edit]

Works with: Perl version 5.x
my $a = <>;
my $b = <>;
 
print
"sum: ", $a + $b, "\n",
"difference: ", $a - $b, "\n",
"product: ", $a * $b, "\n",
"integer quotient: ", int($a / $b), "\n",
"remainder: ", $a % $b, "\n",
"exponent: ", $a ** $b, "\n"
;

Perl 6[edit]

Works with: Rakudo version 2015.09
my Int $a = get.floor;
my Int $b = get.floor;
 
say 'sum: ', $a + $b;
say 'difference: ', $a - $b;
say 'product: ', $a * $b;
say 'integer quotient: ', $a div $b;
say 'remainder: ', $a % $b;
say 'exponentiation: ', $a**$b;

Note that div doesn't always do integer division; it performs the operation "most appropriate to the operand types". Synopsis 3 guarantees that div "on built-in integer types is equivalent to taking the floor of a real division". If you want integer division with other types, say floor($a/$b).

Phix[edit]

integer a = floor(prompt_number("a = ",{}))
integer b = floor(prompt_number("b = ",{}))
 
printf(1,"a + b = %d\n", a+b)
printf(1,"a - b = %d\n", a-b)
printf(1,"a * b = %d\n", a*b)
printf(1,"a / b = %g\n", a/b) -- does not truncate
printf(1,"remainder(a,b) = %d\n", remainder(a,b)) -- same sign as first operand
printf(1,"power(a,b) = %g\n", power(a,b))
Output:
a = 2
b = 3
a + b = 5
a - b = -1
a * b = 6
a / b = 0.666667
remainder(a,b) = 2
power(a,b) = 8

PHL[edit]

module arith;
 
extern printf;
extern scanf;
 
@Integer main [
@Pointer<@Integer> a = alloc(4);
@Pointer<@Integer> b = alloc(4);
scanf("%i %i", a, b);
 
printf("a + b = %i\n", a::get + b::get);
printf("a - b = %i\n", a::get - b::get);
printf("a * b = %i\n", a::get * b::get);
printf("a / b = %i\n", a::get / b::get);
printf("a % b = %i\n", a::get % b::get);
printf("a ** b = %i\n", a::get ** b::get);
 
return 0;
]

PHP[edit]

<?php
$a = fgets(STDIN);
$b = fgets(STDIN);
 
echo
"sum: ", $a + $b, "\n",
"difference: ", $a - $b, "\n",
"product: ", $a * $b, "\n",
"truncating quotient: ", (int)($a / $b), "\n",
"flooring quotient: ", floor($a / $b), "\n",
"remainder: ", $a % $b, "\n";
?>

PicoLisp[edit]

(de math (A B)
(prinl "Add " (+ A B))
(prinl "Subtract " (- A B))
(prinl "Multiply " (* A B))
(prinl "Divide " (/ A B)) # Trucates towards zero
(prinl "Div/rnd " (*/ A B)) # Rounds to next integer
(prinl "Modulus " (% A B)) # Sign of the first operand
(prinl "Power " (** A B)) )

Piet[edit]

PietArithmaticInteger.png


command   stack
in(int)   A
duplicate AA
duplicate AAA
duplicate AAAA
duplicate AAAAA
in(int)   BAAAAA
duplicate BBAAAAA
duplicate BBBAAAAA
duplicate BBBBAAAAA
duplicate BBBBBAAAAA
push 9    9BBBBBAAAAA
push 1    19BBBBBAAAAA
roll      BBBBAAAABA
push 7    7BBBBAAAABA
push 1    17BBBBAAAABA
roll      BBBAAABABA
push 5    5BBBAAABABA
push 1    15BBBAAABABA
roll      BBAABABABA
push 3    3BBAABABABA
push 1    13BBAABABABA
roll      BABABABABA
add       (A+B)BABABABA
out(int)  BABABABA
sub       (A-B)BABABA
out(int)  BABABA
mult      (A*B)BABA
out(int)  BABA
divide    (A/B)BA
out(int)  BA
mod       (A%B)
out(int)  NULL
push 1    1
exit

How rounding is handled is up to the interpreter, but I believe the intent was round towards 0.

PL/I[edit]

 
get list (a, b);
put skip list (a+b);
put skip list (a-b);
put skip list (a*b);
put skip list (trunc(a/b)); /* truncates towards zero. */
put skip list (mod(a, b)); /* Remainder is always positive. */
put skip list (rem(a, b)); /* Sign can be negative. */

Pop11[edit]

;;; Setup token reader
vars itemrep;
incharitem(charin) -> itemrep;
;;; read the numbers
lvars a = itemrep(), b = itemrep();
;;; Print results
printf(a + b, 'a + b = %p\n');
printf(a - b, 'a - b = %p\n');
printf(a * b, 'a * b = %p\n');
printf(a div b, 'a div b = %p\n');
printf(a mod b, 'a mod b = %p\n');

PostScript[edit]

/arithInteger {
/x exch def
/y exch def
x y add =
x y sub =
x y mul =
x y idiv =
x y mod =
x y exp =
} def

PowerShell[edit]

$a = [int] (Read-Host First Number)
$b = [int] (Read-Host Second Number)
 
Write-Host "Sum: $($a + $b)"
Write-Host "Difference: $($a - $b)"
Write-Host "Product: $($a * $b)"
Write-Host "Quotient: $($a / $b)"
Write-Host "Quotient, round to even: $([Math]::Round($a / $b))"
Write-Host "Remainder, sign follows first: $($a % $b)"

Numbers are automatically converted to accomodate for the result. This means not only that Int32 will be expanded to Int64 but also that a non-integer quotient will cause the result to be of a floating-point type.

The remainder has the sign of the first operand.

No exponentiation operator exists, but can be worked around with the .NET BCL:

[Math]::Pow($a, $b)

ProDOS[edit]

IGNORELINE Note: This example includes the math module.
include arithmeticmodule
:a
editvar /newvar /value=a /title=Enter first integer:
editvar /newvar /value=b /title=Enter second integer:
editvar /newvar /value=c
do add -a-,-b-=-c-
printline -c-
do subtract a,b
printline -c-
do multiply a,b
printline -c-
do divide a,b
printline -c-
do modulus a,b
printline -c-
editvar /newvar /value=d /title=Do you want to calculate more numbers?
if -d- /hasvalue yes goto :a else goto :end
:end
IGNORELINE Note: This example does not use the math module.
:a
editvar /newvar /value=a /title=Enter first integer:
editvar /newvar /value=b /title=Enter second integer:
editvar /newvar /value=-a-+-b-=-c-
printline -c-
editvar /newvar /value=a*b=c
printline -c-
editvar /newvar /value=a/b=c
printline -c-
editvar /newvar /value=a %% b=c
printline -c-
editvar /newvar /value=d /title=Do you want to calculate more numbers?
if -d- /hasvalue yes goto :a else goto :end
:end

Prolog[edit]

Integer quotient (`//`) rounds towards 0.

Remainder (`rem`) matches the sign of its first operand.

 
 
print_expression_and_result(M, N, Operator) :-
Expression =.. [Operator, M, N],
Result is Expression,
format('~w ~8|is ~d~n', [Expression, Result]).
 
arithmetic_integer :-
read(M),
read(N),
maplist( print_expression_and_result(M, N), [+,-,*,//,rem,^] ).
 
 

Use thus:

 
?- arithmetic_integer.
|: 5.
|: 7.
5+7 is 12
5-7 is -2
5*7 is 35
5//7 is 0
5 rem 7 is 5
5^7 is 78125
true.
 

PureBasic[edit]

OpenConsole()
 
Define a, b
 
Print("Number 1: "): a = Val(Input())
Print("Number 2: "): b = Val(Input())
 
PrintN("Sum: " + Str(a + b))
PrintN("Difference: " + Str(a - b))
PrintN("Product: " + Str(a * b))
PrintN("Quotient: " + Str(a / b)) ; Integer division (rounding mode=truncate)
PrintN("Remainder: " + Str(a % b))
PrintN("Power: " + Str(Pow(a, b)))
 
Input()
 
CloseConsole()

Python[edit]

x = int(raw_input("Number 1: "))
y = int(raw_input("Number 2: "))
 
print "Sum: %d" % (x + y)
print "Difference: %d" % (x - y)
print "Product: %d" % (x * y)
print "Quotient: %d" % (x / y) # or x // y for newer python versions.
# truncates towards negative infinity
print "Remainder: %d" % (x % y) # same sign as second operand
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
raw_input( )

Notes: In Python3 raw_input() will be renamed to input() (the old input() built-in will go away, though one could use eval(input()) to emulate the old ... and ill-advised ... behavior). Also a better program would wrap the attempted int() conversions in a try: ... except ValueError:... construct such as:

def getnum(prompt):
while True: # retrying ...
try:
n = int(raw_input(prompt))
except ValueError:
print "Input could not be parsed as an integer. Please try again."\
continue
break
return n
 
x = getnum("Number1: ")
y = getnum("Number2: ")
...

(In general it's good practice to perform parsing of all input in exception handling blocks. This is especially true of interactive user input, but also applies to data read from configuration and other files, and marshaled from other processes via any IPC mechanism).

Python also has the procedure divmod that returns both quotient and remainder. eg

quotient, remainder = divmod(355,113)

Giving a quotient of 3, and a remainder of 16.

Python 3.0 compatible code[edit]

def arithmetic(x, y):
for op in "+ - * // % **".split():
expr = "%(x)s %(op)s %(y)s" % vars()
print("%s\t=> %s" % (expr, eval(expr)))
 
 
arithmetic(12, 8)
arithmetic(input("Number 1: "), input("Number 2: "))
Output:
12 + 8  => 20
12 - 8  => 4
12 * 8  => 96
12 // 8 => 1
12 % 8  => 4
12 ** 8	=> 429981696
Number 1: 20
Number 2: 4
20 + 4  => 24
20 - 4  => 16
20 * 4  => 80
20 // 4 => 5
20 % 4  => 0
20 ** 4 => 160000

R[edit]

cat("insert number ")
a <- scan(nmax=1, quiet=TRUE)
cat("insert number ")
b <- scan(nmax=1, quiet=TRUE)
print(paste('a+b=', a+b))
print(paste('a-b=', a-b))
print(paste('a*b=', a*b))
print(paste('a%/%b=', a%/%b))
print(paste('a%%b=', a%%b))
print(paste('a^b=', a^b))
 

Racket[edit]

 
#lang racket/base
 
(define (arithmetic x y)
(for ([op (list + - * / quotient remainder modulo max min gcd lcm)])
(printf "~s => ~s\n" `(,(object-name op) ,x ,y) (op x y))))
 
(arithmetic 8 12)
 
Output:
(+ 8 12) => 20
(- 8 12) => -4
(* 8 12) => 96
(/ 8 12) => 2/3
(quotient 8 12) => 0
(remainder 8 12) => 8
(modulo 8 12) => 8
(max 8 12) => 12
(min 8 12) => 8
(gcd 8 12) => 4
(lcm 8 12) => 24

Raven[edit]

'  Number 1: ' print expect 0 prefer as x
' Number 2: ' print expect 0 prefer as y
 
x y + " sum: %d\n" print
x y - "difference: %d\n" print
x y * " product: %d\n" print
x y / " quotient: %d\n" print
x y % " remainder: %d\n" print

REBOL[edit]

rebol [
Title: "Integer"
Author: oofoe
Date: 2010-09-29
URL: http://rosettacode.org/wiki/Arithmetic/Integer
]

 
x: to-integer ask "Please type in an integer, and press [enter]: "
y: to-integer ask "Please enter another integer: "
print ""
 
print ["Sum:" x + y]
print ["Difference:" x - y]
print ["Product:" x * y]
 
print ["Integer quotient (coercion)  :" to-integer x / y]
print ["Integer quotient (away from zero)  :" round x / y]
print ["Integer quotient (halves round towards even digits)  :" round/even x / y]
print ["Integer quotient (halves round towards zero)  :" round/half-down x / y]
print ["Integer quotient (round in negative direction)  :" round/floor x / y]
print ["Integer quotient (round in positive direction)  :" round/ceiling x / y]
print ["Integer quotient (halves round in positive direction):" round/half-ceiling x / y]
 
print ["Remainder:" r: x // y]
 
; REBOL evaluates infix expressions from left to right. There are no
; precedence rules -- whatever is first gets evaluated. Therefore when
; performing this comparison, I put parens around the first term
; ("sign? a") of the expression so that the value of /a/ isn't
; compared to the sign of /b/. To make up for it, notice that I don't
; have to use a specific return keyword. The final value in the
; function is returned automatically.
 
match?: func [a b][(sign? a) = sign? b]
 
result: copy []
if match? r x [append result "first"]
if match? r y [append result "second"]
 
; You can evaluate arbitrary expressions in the middle of a print, so
; I use a "switch" to provide a more readable result based on the
; length of the /results/ list.
 
print [
"Remainder sign matches:"
switch length? result [
0 ["neither"]
1 [result/1]
2 ["both"]
]
]
 
print ["Exponentiation:" x ** y]
Output:
Please type in an integer, and press [enter]: 17
Please enter another integer: -4

Sum: 13
Difference: 21
Product: -68
Integer quotient (coercion)                          : -4
Integer quotient (away from zero)                    : -4
Integer quotient (halves round towards even digits)  : -4
Integer quotient (halves round towards zero)         : -4
Integer quotient (round in negative direction)       : -5
Integer quotient (round in positive direction)       : -4
Integer quotient (halves round in positive direction): -4
Remainder: 1
Remainder sign matches: first
Exponentiation: 1.19730367213036E-5

Retro[edit]

Retro's arithmetic functions are based on those in Forth. The example is an adaption of the one from Forth.

: arithmetic ( ab- )
over "\na = %d" puts
dup "\nb = %d" puts
2over + "\na + b = %d" puts
2over - "\na - b = %d" puts
2over * "\na * b = %d" puts
/mod "\na / b = %d" puts
"\na mod b = %d\n" puts ;

REXX[edit]

All operators automatically produce integers   (up to 20 decimal digits in the program below),   or
numbers in exponential format when necessary.

/*REXX program obtains two integers from the C.L. (a prompt);  displays some operations.*/
numeric digits 20 /*#s are round at 20th significant dig.*/
parse arg x y . /*maybe the integers are on the C.L. */
 
do while \datatype(x,'W') | \datatype(y,'W') /*both X and Y must be integers. */
say "─────Enter two integer values (separated by blanks):"
parse pull x y . /*accept two thingys from command line.*/
end /*while*/
/* [↓] perform this DO loop twice. */
do j=1 for 2 /*show A oper B, then B oper A.*/
call show 'addition' , "+", x+y
call show 'subtraction' , "-", x-y
call show 'multiplication' , "*", x*y
call show 'int division' , "%", x%y, ' [rounds down]'
call show 'real division' , "/", x/y
call show 'division remainder', "//", x//y, ' [sign from 1st operand]'
call show 'power' , "**", x**y
 
parse value x y with y x /*swap the two values and perform again*/
if j==1 then say copies('═', 79) /*display a fence after the 1st round. */
end /*j*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: parse arg c,o,#,?; say right(c,25)' ' x center(o,4) y " ───► " #  ?; return

output when using the input of:   17   -4

                 addition  4  +   -17  ───►  -13
              subtraction  4  -   -17  ───►  21
           multiplication  4  *   -17  ───►  -68
            int  division  4  %   -17  ───►  0     [rounds down]
            real division  4  /   -17  ───►  -0.23529411764705882353
       division remainder  4  //  -17  ───►  4     [sign from 1st operand]
                    power  4  **  -17  ───►  5.8207660913467407227E-11
═══════════════════════════════════════════════════════════════════════════════
                 addition  -17  +   4  ───►  -13
              subtraction  -17  -   4  ───►  -21
           multiplication  -17  *   4  ───►  -68
            int  division  -17  %   4  ───►  -4     [rounds down]
            real division  -17  /   4  ───►  -4.25
       division remainder  -17  //  4  ───►  -1     [sign from 1st operand]
                    power  -17  **  4  ───►  83521

Ring[edit]

 
func Test a,b
see "a+b" + ( a + b ) + nl
see "a-b" + ( a - b ) + nl
see "a*b" + ( a * b ) + nl
// The quotient isn't integer, so we use the Ceil() function, which truncates it downward.
see "a/b" + Ceil( a / b ) + nl
// Remainder:
see "a%b" + ( a % b ) + nl
see "a**b" + pow(a,b ) + nl
 

Ruby[edit]

puts 'Enter x and y'
x = gets.to_i # to check errors, use x=Integer(gets)
y = gets.to_i
 
puts "Sum: #{x+y}",
"Difference: #{x-y}",
"Product: #{x*y}",
"Quotient: #{x/y}", # truncates towards negative infinity
"Quotient: #{x.fdiv(y)}", # float
"Remainder: #{x%y}", # same sign as second operand
"Exponentiation: #{x**y}"

Run BASIC[edit]

input "1st integer: "; i1
input "2nd integer: "; i2
 
print " Sum"; i1 + i2
print " Diff"; i1 - i2
print " Product"; i1 * i2
if i2 <>0 then print " Quotent "; int( i1 / i2); else print "Cannot divide by zero."
print "Remainder"; i1 MOD i2
print "1st raised to power of 2nd"; i1 ^ i2

Rust[edit]

Note that this code cannot be run within the Rust playpen as it does not support console input.

use std::env;
 
fn main() {
let args: Vec<_> = env::args().collect();
let a = args[1].parse::<i32>().unwrap();
let b = args[2].parse::<i32>().unwrap();
 
println!("sum: {}", a + b);
println!("difference: {}", a - b);
println!("product: {}", a * b);
println!("integer quotient: {}", a / b); // truncates towards zero
println!("remainder: {}", a % b); // same sign as first operand
}

Sass/SCSS[edit]

 
@function arithmetic($a,$b) {
@return $a + $b, $a - $b, $a * $b, ($a - ($a % $b))/$b, $a % $b;
}
 

Which you use with:

 
nth(arithmetic(10,3),1);
 

Or each of the functions separately:

 
@function sum($a,$b) {
@return $a + $b;
}
 
@function difference($a,$b) {
@return $a - $b;
}
 
@function product($a,$b) {
@return $a * $b;
}
 
@function integer-division($a,$b) {
@return ($a - ($a % $b))/$b;
}
 
@function remainder($a,$b) {
@return $a % $b;
}
 
@function float-division($a,$b) {
@return $a / $b;
}
 

Scala[edit]

val a = Console.readInt
val b = Console.readInt
 
val sum = a + b //integer addition is discouraged in print statements due to confusion with String concatenation
println("a + b = " + sum)
println("a - b = " + (a - b))
println("a * b = " + (a * b))
println("quotient of a / b = " + (a / b)) // truncates towards 0
println("remainder of a / b = " + (a % b)) // same sign as first operand

Scheme[edit]

(define (arithmetic x y)
(for-each (lambda (op)
(write (list op x y))
(display " => ")
(write ((eval op) x y))
(newline))
'(+ - * / quotient remainder modulo max min gcd lcm)))
 
(arithmetic 8 12)

quotient - truncates towards 0 remainder - same sign as first operand modulo - same sign as second operand

 prints this:

(+ 8 12) => 20
(- 8 12) => -4
(* 8 12) => 96
(/ 8 12) => 2/3
(quotient 8 12) => 0
(remainder 8 12) => 8
(modulo 8 12) => 8
(max 8 12) => 12
(min 8 12) => 8
(gcd 8 12) => 4
(lcm 8 12) => 24

Seed7[edit]

$ include "seed7_05.s7i";
 
const proc: main is func
local
var integer: a is 0;
var integer: b is 0;
begin
write("a = ");
readln(a);
write("b = ");
readln(b);
 
writeln("a + b = " <& a + b);
writeln("a - b = " <& a - b);
writeln("a * b = " <& a * b);
writeln("a div b = " <& a div b); # Rounds towards zero
writeln("a rem b = " <& a rem b); # Sign of the first operand
writeln("a mdiv b = " <& a mdiv b); # Rounds towards negative infinity
writeln("a mod b = " <& a mod b); # Sign of the second operand
end func;

Sidef[edit]

var a = Sys.scanln("First number: ").to_i;
var b = Sys.scanln("Second number: ").to_i;
 
%w'+ - * // % ** ^ | & << >>'.each { |op|
"#{a} #{op} #{b} = #{a.$op(b)}".say;
}
Output:
First number: 1234
Second number: 7
1234 + 7 = 1241
1234 - 7 = 1227
1234 * 7 = 8638
1234 // 7 = 176
1234 % 7 = 2
1234 ** 7 = 4357186184021382204544
1234 ^ 7 = 1237
1234 | 7 = 1239
1234 & 7 = 2
1234 << 7 = 157952
1234 >> 7 = 9

Slate[edit]

[| :a :b |
inform: (a + b) printString.
inform: (a - b) printString.
inform: (a * b) printString.
inform: (a / b) printString.
inform: (a // b) printString.
inform: (a \\ b) printString.
 
] applyTo: {Integer readFrom: (query: 'Enter a: '). Integer readFrom: (query: 'Enter b: ')}.

Smalltalk[edit]

Works with: GNU Smalltalk
| a b |
'Input number a: ' display.
a := (stdin nextLine) asInteger.
'Input number b: ' display.
b := (stdin nextLine) asInteger.
('a+b=%1' % { a + b }) displayNl.
('a-b=%1' % { a - b }) displayNl.
('a*b=%1' % { a * b }) displayNl.
('a/b=%1' % { a // b }) displayNl.
('a%%b=%1' % { a \\ b }) displayNl.

SNOBOL4[edit]

 
output = "Enter first integer:"
first = input
output = "Enter second integer:"
second = input
output = "sum = " first + second
output = "diff = " first - second
output = "prod = " first * second
output = "quot = " (qout = first / second)
output = "rem = " first - (qout * second)
end

SNUSP[edit]

As a BF derivative, SNUSP only has increment and decrement as native operations. Here are routines for other basic arithmetic upon single digit numbers and results.

See also: Ethiopian Multiplication

$\
,
@
\=@@@-@-----# atoi
>
,
@
\=@@@-@-----#
<
@ # 4 copies
\=!/?!/->>+>>+>>+>>+<<<<<<<<?\#
> | #\?<<<<<<<<+>>+>>+>>+>>-/
@ |
\==/
\>>>>\
/>>>>/
@
\==!/===?\# add
< \>+<-/
@
\=@@@+@+++++# itoa
.
<
@
\==!/===?\# subtract
< \>-<-/
@
\=@@@+@+++++#
.
 !
/\
 ?- multiply
\/ #/?<<+>+>-==\ /==-<+<+>>?\# /==-<<+>>?\#
< \->+>+<<!/?/# #\?\!>>+<+<-/ #\?\!>>+<<-/
@ /==|=========|=====\ /-\ |
\======<?!/>@/<-?!\>>>@/<<<-?\=>!\?/>!/@/<#
< \=======|==========/ /-\ |
@ \done======>>>!\?/<=/
\=@@@+@+++++#
.
 !
/\
 ?- zero
\/
< divmod
@ /-\
\?\<!\?/#!===+<<<\ /-\
| \<==@\>@\>>!/?!/=<?\>!\?/<<#
| | | #\->->+</
| \=!\=?!/->>+<<?\#
@ #\?<<+>>-/
\=@@@+@+++++#
.
<
@
\=@@@+@+++++#
.
#

Standard ML[edit]

val () = let
val a = valOf (Int.fromString (valOf (TextIO.inputLine TextIO.stdIn)))
val b = valOf (Int.fromString (valOf (TextIO.inputLine TextIO.stdIn)))
in
print ("a + b = " ^ Int.toString (a + b) ^ "\n");
print ("a - b = " ^ Int.toString (a - b) ^ "\n");
print ("a * b = " ^ Int.toString (a * b) ^ "\n");
print ("a div b = " ^ Int.toString (a div b) ^ "\n"); (* truncates towards negative infinity *)
print ("a mod b = " ^ Int.toString (a mod b) ^ "\n"); (* same sign as second operand *)
print ("a quot b = " ^ Int.toString (Int.quot (a, b)) ^ "\n");(* truncates towards 0 *)
print ("a rem b = " ^ Int.toString (Int.rem (a, b)) ^ "\n"); (* same sign as first operand *)
print ("~a = " ^ Int.toString (~a) ^ "\n") (* unary negation, unusual notation compared to other languages *)
end

Tcl[edit]

puts "Please enter two numbers:"
 
set x [expr {int([gets stdin])}]; # Force integer interpretation
set y [expr {int([gets stdin])}]; # Force integer interpretation
 
puts "$x + $y = [expr {$x + $y}]"
puts "$x - $y = [expr {$x - $y}]"
puts "$x * $y = [expr {$x * $y}]"
puts "$x / $y = [expr {$x / $y}]"
puts "$x mod $y = [expr {$x % $y}]"
puts "$x 'to the' $y = [expr {$x ** $y}]"

Since Tcl doesn't really know about the "type" of a variable, the "expr" command is used to declare whatever follows as an "expression". This means there is no such thing as "integer arithmetic" and hence the kludge with int([gets stdin]).

Often, these operations would be performed in a different way from what is shown here. For example, to increase the variable "x" by the value of the variable "y", one would write

incr x $y

Also, it's important to surround the arguments to the expr in braces, especially when any of the parts of the expression are not literal constants. Discussion of this is on The Tcler's Wiki.

TI-83 BASIC[edit]

Pauses added due to TI-83's lack of screen size.

 
Prompt A,B
Disp "SUM"
Pause A+B
Disp "DIFFERENCE"
Pause A-B
Disp "PRODUCT"
Pause AB
Disp "INTEGER QUOTIENT"
Pause int(A/B)
Disp "REMAINDER"
Pause A-B*int(A/B)
 

TI-89 BASIC[edit]

Local a, b
Prompt a, b
Disp "Sum: " & string(a + b)
Disp "Difference: " & string(a - b)
Disp "Product: " & string(a * b)
Disp "Integer quotient: " & string(intDiv(a, b))
Disp "Remainder: " & string(remain(a, b))

Toka[edit]

[ ( a b -- )
2dup ." a+b = " + . cr
2dup ." a-b = " - . cr
2dup ." a*b = " * . cr
2dup ." a/b = " / . ." remainder " mod . cr
] is mathops

TUSCRIPT[edit]

 
$$ MODE TUSCRIPT
a=5
b=3
c=a+b
c=a-b
c=a*b
c=a/b
c=a%b
 
Output:
a=5
b=3
c=a+b
c            = 8
c=a-b
c            = 2
c=a*b
c            = 15
c=a/b
c            = 1
c=a%b
c            = 2

UNIX Shell[edit]

The Unix shell does not directly support arithmetic operations, so external tools, such as expr are used to perform arithmetic calculations when required:

Works with: Bourne Shell
Works with: Almquist SHell
#!/bin/sh
read a; read b;
echo "a+b = " `expr $a + $b`
echo "a-b = " `expr $a - $b`
echo "a*b = " `expr $a \* $b`
echo "a/b = " `expr $a / $b` # truncates towards 0
echo "a mod b = " `expr $a % $b` # same sign as first operand

Notes: Using the ` (backtick operators, also available in most Bourne shells via the $(...) syntax) allows us to keep the results on their labels in the most efficient and portable way. The spaces around the operators in the expr command line arguments are required and the shell requires us to quote or escape the * character has shown, to prevent any possible "globbing" --- filename expansion of the * as a wildcard character.

With SUSv3 parameter expansions:

Works with: Bourne Again SHell version 3.2
Works with: pdksh version 5.2.14
Works with: Z SHell
#!/bin/sh
read a; read b;
echo "a+b = $((a+b))"
echo "a-b = $((a-b))"
echo "a*b = $((a*b))"
echo "a/b = $((a/b))" # truncates towards 0
echo "a mod b = $((a%b))" # same sign as first operand

Note: spaces inside the $((...)) are optional and not required; the $((...)) can be inside or outside the double quotes, but the `...` expressions from the previous example can also be inside or outside the double quotes.

Ursa[edit]

#
# integer arithmetic
#
 
decl int x y
out "number 1: " console
set x (in int console)
out "number 2: " console
set y (in int console)
 
out "\nsum:\t" (int (+ x y)) endl console
out "diff:\t" (int (- x y)) endl console
out "prod:\t" (int (* x y)) endl console
# quotient doesn't round at all, but the int function rounds up
out "quot:\t" (int (/ x y)) endl console
# mod takes the sign of x
out "mod:\t" (int (mod x y)) endl console

Sample session:

number 1: 15
number 2: 7

sum:	22
diff:	8
prod:	105
quot:	2
mod:	1

VBA[edit]

 
'Arithmetic - Integer
Sub RosettaArithmeticInt()
Dim opr As Variant, a As Integer, b As Integer
On Error Resume Next
 
a = CInt(InputBox("Enter first integer", "XLSM | Arithmetic"))
b = CInt(InputBox("Enter second integer", "XLSM | Arithmetic"))
 
Debug.Print "a ="; a, "b="; b, vbCr
For Each opr In Split("+ - * / \ mod ^", " ")
Select Case opr
Case "mod": Debug.Print "a mod b", a; "mod"; b, a Mod b
Case "\": Debug.Print "a \ b", a; "\"; b, a \ b
Case Else: Debug.Print "a "; opr; " b", a; opr; b, Evaluate(a & opr & b)
End Select
Next opr
End Sub
 

VBScript[edit]

VBScript's variables are all Variants. What starts out as an integer may be converted to something else if the need arises.

Implementation[edit]

option explicit
dim a, b
wscript.stdout.write "A? "
a = wscript.stdin.readline
wscript.stdout.write "B? "
b = wscript.stdin.readline
 
a = int( a )
b = int( b )
 
wscript.echo "a + b=", a + b
wscript.echo "a - b=", a - b
wscript.echo "a * b=", a * b
wscript.echo "a / b=", a / b
wscript.echo "a \ b=", a \ b
wscript.echo "a mod b=", a mod b
wscript.echo "a ^ b=", a ^ b

Another Implementation[edit]

Gives the same output for the same input. Inspired by Python version.

option explicit
dim a, b
wscript.stdout.write "A? "
a = wscript.stdin.readline
wscript.stdout.write "B? "
b = wscript.stdin.readline
 
a = int( a )
b = int( b )
 
dim op
for each op in split("+ - * / \ mod ^", " ")
wscript.echo "a",op,"b=",eval( "a " & op & " b")
next

Invocation[edit]

C:\foo>arithmetic.vbs
A? 45
B? 11
a + b= 4511
a - b= 34
a * b= 495
a / b= 4.09090909090909
a \ b= 4
a mod b= 1
a ^ b= 1.5322783012207E+18

Vedit macro language[edit]

#1 = Get_Num("Give number a: ")
#2 = Get_Num("Give number b: ")
Message("a + b = ") Num_Type(#1 + #2)
Message("a - b = ") Num_Type(#1 - #2)
Message("a * b = ") Num_Type(#1 * #2)
Message("a / b = ") Num_Type(#1 / #2)
Message("a % b = ") Num_Type(#1 % #2)

Vim Script[edit]

let a = float2nr(input("Number 1: ") + 0)
let b = float2nr(input("Number 2: ") + 0)
echo "\nSum: " . (a + b)
echo "Difference: " . (a - b)
echo "Product: " . (a * b)
" The result of an integer division is truncated

echo "Quotient: " . (a / b)
" The sign of the result of the remainder operation matches the sign of

" the first operand

echo "Remainder: " . (a % b)

Visual Basic .NET[edit]

Imports System.Console
Module Module1
Sub Main
Dim a = CInt(ReadLine)
Dim b = CInt(ReadLine)
WriteLine("Sum " & a + b)
WriteLine("Difference " & a - b)
WriteLine("Product " & a - b)
WriteLine("Quotient " & a / b)
WriteLine("Integer Quotient " & a \ b)
WriteLine("Remainder " & a Mod b)
WriteLine("Exponent " & a ^ b)
End Sub
End Module

Wart[edit]

a <- (read)
b <- (read)
prn "sum: " a+b
prn "difference: " a-b
prn "product: " a*b
prn "quotient: " a/b
prn "integer quotient: " (int a/b)
prn "remainder: " a%b
prn "exponent: " a^b

Wren[edit]

 
import "io" for Stdin
var a = Num.fromString(Stdin.readLine())
var b = Num.fromString(Stdin.readLine())
System.print("sum:  %(a + b)")
System.print("difference:  %(a - b)")
System.print("product:  %(a * b)")
System.print("integer quotient: %((a / b).floor)")
System.print("remainder:  %(a % b)")
 

x86 Assembly[edit]

Input and output would be OS-specific and are not implemented. This routine works on the 16-bit 8086, as well as on its 32-bit and 64-bit successors: it could be trivially modified to perform 32-bit or 64-bit arithmetic on machines where those are supported. The quotient is truncated towards zero; the remainder takes its sign from the first operand.

arithm: mov      cx,          a
mov bx, b
xor dx, dx
 
mov ax, cx
add ax, bx
mov sum, ax
 
mov ax, cx
imul bx
mov product, ax
 
mov ax, cx
sub ax, bx
mov difference, ax
 
mov ax, cx
idiv bx
mov quotient, ax
mov remainder, dx
 
ret

XLISP[edit]

(DEFUN INTEGER-ARITHMETIC ()
(DISPLAY "Enter two integers separated by a space.")
(NEWLINE)
(DISPLAY "> ")
(DEFINE A (READ))
(DEFINE B (READ))
(DISPLAY `(SUM ,(+ A B)))
(NEWLINE)
(DISPLAY `(DIFFERENCE ,(- A B)))
(NEWLINE)
(DISPLAY `(PRODUCT ,(* A B)))
(NEWLINE)
(DISPLAY `(QUOTIENT ,(QUOTIENT A B))) ; truncates towards zero
(NEWLINE)
(DISPLAY `(REMAINDER ,(REM A B))) ; takes sign of first operand
(NEWLINE)
(DISPLAY `(EXPONENTIATION ,(EXPT A B))))

XPL0[edit]

include c:\cxpl\codes;
int A, B;
[A:= IntIn(0);
B:= IntIn(0);
IntOut(0, A+B); CrLf(0);
IntOut(0, A-B); CrLf(0);
IntOut(0, A*B); CrLf(0);
IntOut(0, A/B); CrLf(0); \truncates toward zero
IntOut(0, rem(0)); CrLf(0); \remainder's sign matches first operand (A)
]

XSLT[edit]

<xsl:template name="arithmetic">
<xsl:param name="a">5</xsl:param>
<xsl:param name="b">2</xsl:param>
<fo:block>a + b = <xsl:value-of select="$a + $b"/></fo:block>
<fo:block>a - b = <xsl:value-of select="$a - $b"/></fo:block>
<fo:block>a * b = <xsl:value-of select="$a * $b"/></fo:block>
<fo:block>a / b = <xsl:value-of select="round($a div $b)"/></fo:block>
<fo:block>a mod b = <xsl:value-of select="$a mod $b"/></fo:block>
</xsl:template>

Yorick[edit]

x = y = 0;
read, x, y;
write, "x + y =", x + y;
write, "x - y =", x - y;
write, "x * y =", x * y;
write, "x / y =", x / y; // rounds toward zero
write, "x % y =", x % y; // remainder; matches sign of first operand when operands' signs differ
write, "x ^ y =", x ^ y; // exponentiation

zkl[edit]

x,y:=ask("Two ints: ").split(" ").apply("toInt");
println("x+y = ",x + y);
println("x-y = ",x - y);
println("x*y = ",x * y);
println("x/y = ",x / y); // rounds toward zero
println("x%y = ",x % y); // remainder; matches sign of first operand when operands' signs differ
println("x.divr(y) = ",x.divr(y)); // (x/y,remainder); sign as above

ZX Spectrum Basic[edit]

5 LET a=5: LET b=3
10 PRINT a;" + ";b;" = ";a+b
20 PRINT a;" - ";b;" = ";a-b
30 PRINT a;" * ";b;" = ";a*b
40 PRINT a;" / ";b;" = ";INT (a/b)
50 PRINT a;" mod ";b;" = ";a-INT (a/b)*b
60 PRINT a;" to the power of ";b;" = ";a^b