Arithmetic/Integer: Difference between revisions

no edit summary
No edit summary
Line 4:
 
=={{header|Ada}}==
<lang ada>with Ada.Text_Io;
with Ada.Integer_Text_IO;
 
Line 19:
Put_Line("a*b = " & Integer'Image(A * B));
Put_Line("a/b = " & Integer'Image(A / B) & ", remainder " & Integer'Image(A mod B));
end Integer_Arithmetic;</adalang>
 
=={{header|ALGOL 68}}==
Line 62:
 
=={{header|C}}==
<lang c>#include <stdio.h>
#include <stdlib.h>
 
Line 78:
printf("a%%b = %d\n", a%b);
return 0;
}</clang>
 
=={{header|C++}}==
<lang cpp>#include <iostream>
 
int main()
Line 92:
std::cout << "a/b = " << a/b << ", remainder " << a%b << "\n";
return 0;
}</cpplang>
 
=={{header|C sharp|C#}}==
<lang csharp>using System;
 
class Program
Line 114:
Console.WriteLine("a % b = {0}", a % b);
}
}</csharplang>
 
=={{header|Common Lisp}}==
 
<lang lisp>(defun arithmetic ()
(let ((a (read *query-io*)) (b (read *query-io*)))
(format t "a + b = ~a~%" (+ a b))
Line 124:
(format t "a * b = ~a~%" (* a b))
(format t "a / b = ~a~%" (/ a b))
(format t "a % b = ~a~%" (mod a b))))</lisplang>
 
=={{header|D}}==
 
<lang d>import std.stdio, std.string;
 
void main()
Line 138:
writefln("a / b = ", a/b);
writefln("a % b = ", a%b);
}</dlang>
 
=={{header|DOS Batch File}}==
Line 245:
 
=={{header|Java}}==
<lang java>import java.util.Scanner;
 
public static void main(String[] args){
Line 258:
System.out.println("quotient of a / b = " + (a / b));//integer division truncates decimal places
System.out.println("remainder of a / b = " + (a % b));
}</javalang>
 
=={{header|Logo}}==
Line 296:
 
=={{header|OCaml}}==
<lang ocaml>let _ =
let a = read_int ()
and b = read_int () in
Line 304:
Printf.printf "a * b = %d\n" (a * b);
Printf.printf "a / b = %d\n" (a / b);
Printf.printf "a mod b = %d\n" (a mod b)</ocamllang>
 
=={{header|Pascal}}==
Line 322:
=={{header|Perl}}==
{{works with|Perl|5.x}}
<lang perl>my $a = <>;
my $b = <>;
 
Line 331:
"integer quotient: ", int($a / $b), "\n",
"remainder: ", $a % $b, "\n"
;</perllang>
 
=={{header|Pop11}}==
Line 349:
=={{header|Python}}==
 
<lang python>x = int(raw_input("Number 1: "))
y = int(raw_input("Number 2: "))
 
Line 359:
 
## Only used to keep the display up when the program ends
raw_input( )</pythonlang>
 
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:
 
<lang python>
def getnum(prompt):
while True: # retrying ...
Line 377:
y = getnum("Number2: ")
...
</pythonlang>
 
(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).
Line 386:
 
=== Python 3.0 compatible code ===
<lang python>def arithmetic(x, y):
for op in "+ - * // %".split():
expr = "%(x)s %(op)s %(y)s" % vars()
Line 393:
 
arithmetic(12, 8)
arithmetic(input("Number 1: "), input("Number 2: "))</pythonlang>
Output:
<pre>12 + 8 => 20
Line 421:
=={{header|Ruby}}==
 
<lang ruby>puts 'Enter x and y'
x=gets.to_i
y=gets.to_i
Line 429:
"Product: #{x*y}",
"Quotient: #{x/y}",
"Remainder: #{x%y}"</rubylang>
 
=={{header|Scheme}}==
 
<lang scheme>(define (arithmetic x y)
(for-each (lambda (op)
(write (list op x y))
Line 441:
'(+ - * / quotient remainder modulo max min gcd lcm)))
(arithmetic 8 12)</schemelang>
prints this: