Arithmetic/Integer: Difference between revisions

Content added Content deleted
(Pari/GP)
m (whitespace, minor corrections)
Line 1: Line 1:
{{Task|Basic language learning}}[[Category:Arithmetic operations]]{{basic data operation}}Get two integers from the user, and then output the sum, difference, product, integer quotient and remainder of those numbers. Don't include error handling. For quotient, indicate how it rounds (e.g. towards 0, 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.
{{Task|Basic language learning}}[[Category:Arithmetic operations]]
{{basic data operation}}
Get two integers from the user, and then output the sum, difference, product, integer quotient and remainder of those numbers. Don't include error handling. For quotient, indicate how it rounds (e.g. towards 0, 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.


Also include the exponentiation operator if one exists.
Also include the exponentiation operator if one exists.
Line 78: Line 76:


=={{header|Aikido}}==
=={{header|Aikido}}==
<lang aikido>
<lang aikido>var a = 0

var a = 0
var b = 0
var b = 0
stdin -> a // read int from stdin
stdin -> a // read int from stdin
Line 89: Line 85:
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))
println ("a%b=" + (a % b))</lang>

</lang>


=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
{{trans|C}}
{{trans|C}}

{{works with|ALGOL 68|Revision 1 - no extensions to language used}}
{{works with|ALGOL 68|Revision 1 - no extensions to language used}}

{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]}}
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of FORMATted transput}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of FORMATted transput}}
Line 388: Line 380:


=={{header|Eiffel}}==
=={{header|Eiffel}}==
{{works with|SmartEiffel}} version 2.4
{{works with|SmartEiffel|2.4}}

In a file called main.e:
In a file called main.e:
<lang eiffel>class MAIN
<lang eiffel>class MAIN
Line 631: Line 622:
A to the power of B = 16E-4 </lang>
A to the power of B = 16E-4 </lang>


== Icon and Unicon ==
=={{header|Icon}}==
{{works with|Unicon}}
==={{header|Icon}}===

<lang Icon>procedure main()
<lang Icon>procedure main()
writes("Input 1st integer a := ")
writes("Input 1st integer a := ")
Line 647: Line 637:
write(" a ^ b = ",a^b)
write(" a ^ b = ",a^b)
end</lang>
end</lang>

==={{header|Unicon}}===
This Icon solution works in Unicon.


=={{header|Inform 7}}==
=={{header|Inform 7}}==
Line 716: Line 703:


=={{header|JavaScript}}==
=={{header|JavaScript}}==
{{works with|JScript}}
In order to get user input, this solution {{works with|JScript}} or {{works with|SpiderMonkey}}, however the arithmetic operators are the same for any version of JavaScript.
{{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.
<lang javascript>var a = parseInt(get_input("Enter an integer"), 10);
<lang javascript>var a = parseInt(get_input("Enter an integer"), 10);
var b = parseInt(get_input("Enter an integer"), 10);
var b = parseInt(get_input("Enter an integer"), 10);
Line 933: Line 921:
And 0&2 = 0
And 0&2 = 0
Or 0!2 = 1</lang>
Or 0!2 = 1</lang>



=={{header|NSIS}}==
=={{header|NSIS}}==
All Arithmetic in NSIS is handled by the [http://nsis.sourceforge.net/Docs/Chapter4.html#4.9.10.2 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.
All Arithmetic in NSIS is handled by the [http://nsis.sourceforge.net/Docs/Chapter4.html#4.9.10.2 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.
<lang nsis>
<lang nsis>Function Arithmetic
Function Arithmetic
Push $0
Push $0
Push $1
Push $1
Line 961: Line 947:
Pop $1
Pop $1
Pop $0
Pop $0
FunctionEnd
FunctionEnd</lang>
</lang>


=={{header|Objeck}}==
=={{header|Objeck}}==
<lang objeck>
<lang objeck>bundle Default {
bundle Default {
class Arithmetic {
class Arithmetic {
function : Main(args : System.String[]) ~ Nil {
function : Main(args : System.String[]) ~ Nil {
Line 982: Line 966:
}
}
}
}
}</lang>
}
</lang>


=={{header|OCaml}}==
=={{header|OCaml}}==
Line 1,028: Line 1,011:


=={{header|PARI/GP}}==
=={{header|PARI/GP}}==
<lang>arith(a,b)={
<lang parigp>arith(a,b)={
print(a+b);
print(a+b);
print(a-b);
print(a-b);
Line 1,067: Line 1,050:
=={{header|Perl 6}}==
=={{header|Perl 6}}==
{{works with|Rakudo|#21 "Seattle"}}
{{works with|Rakudo|#21 "Seattle"}}

<lang perl6>my Int $a = floor $*IN.get;
<lang perl6>my Int $a = floor $*IN.get;
my Int $b = floor $*IN.get;
my Int $b = floor $*IN.get;
Line 1,131: Line 1,113:


=={{header|PostScript}}==
=={{header|PostScript}}==
<lang>
<lang ps>/arithInteger {
/x exch def
/arithInteger{
/x exch def
/y exch def
x y add =
/y exch def
x y add =
x y sub =
x y sub =
x y mul =
x y mul =
x y idiv =
x y idiv =
x y mod =
x y mod =
x y exp =
} def</lang>
x y exp =
}def
</lang>
=={{header|PowerShell}}==
=={{header|PowerShell}}==
<lang powershell>$a = [int] (Read-Host First Number)
<lang powershell>$a = [int] (Read-Host First Number)
Line 1,387: Line 1,367:
println("a * b = " + (a * b));
println("a * b = " + (a * b));
println("quotient of a / b = " + (a / b)); // truncates towards 0
println("quotient of a / b = " + (a / b)); // truncates towards 0
println("remainder of a / b = " + (a % b)); // same sign as first operand
println("remainder of a / b = " + (a % b)); // same sign as first operand</lang>
</lang>


=={{header|Scheme}}==
=={{header|Scheme}}==
Line 1,617: Line 1,596:
VBScript's variables are all Variants. What starts out as an integer may be converted to something else if the need arises.
VBScript's variables are all Variants. What starts out as an integer may be converted to something else if the need arises.


=====Implementation=====
===Implementation===
<lang vb>
<lang vb>option explicit
option explicit
dim a, b
dim a, b
wscript.stdout.write "A? "
wscript.stdout.write "A? "
Line 1,635: Line 1,613:
wscript.echo "a \ b=", a \ b
wscript.echo "a \ b=", a \ b
wscript.echo "a mod b=", a mod b
wscript.echo "a mod b=", a mod b
wscript.echo "a ^ b=", a ^ b
wscript.echo "a ^ b=", a ^ b</lang>
</lang>


=====Another Implementation=====
===Another Implementation===
Gives the same output for the same input. Inspired by Python version.
Gives the same output for the same input. Inspired by Python version.
<lang vb>
<lang vb>option explicit
option explicit
dim a, b
dim a, b
wscript.stdout.write "A? "
wscript.stdout.write "A? "
Line 1,654: Line 1,630:
for each op in split("+ - * / \ mod ^", " ")
for each op in split("+ - * / \ mod ^", " ")
wscript.echo "a",op,"b=",eval( "a " & op & " b")
wscript.echo "a",op,"b=",eval( "a " & op & " b")
next
next</lang>
</lang>


=====Invocation=====
===Invocation===
<pre>
<pre>
C:\foo>arithmetic.vbs
C:\foo>arithmetic.vbs