Arithmetic/Integer: Difference between revisions
Content added Content deleted
(Vedit macro language) |
No edit summary |
||
Line 4: | Line 4: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
<ada> |
<ada>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) & ", remainder " & Integer'Image(A mod B)); |
|||
end Integer_Arithmetic;</ada> |
|||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
Line 39: | Line 39: | ||
=={{header|C}}== |
=={{header|C}}== |
||
<c>#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); |
|||
printf("a%%b = %d\n", a%b); |
|||
return 0; |
|||
}</c> |
|||
} |
|||
=={{header|C++}}== |
=={{header|C++}}== |
||
<cpp>#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; |
|||
}</cpp> |
|||
} |
|||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
<csharp>using System; |
|||
class Program |
|||
{ |
|||
static void Main(string[] args) |
|||
{ |
|||
int a, b; |
|||
if (args.Length < 2) |
|||
return; |
|||
b = Convert.ToInt32(args[1]); |
|||
if (b == 0) |
|||
return; |
|||
a = Convert.ToInt32(args[0]); |
|||
Console.WriteLine("a + b = {0}", a + b); |
|||
Console.WriteLine("a - b = {0}", a - b); |
|||
Console.WriteLine("a * b = {0}", a * b); |
|||
Console.WriteLine("a / b = {0}", a / b); |
|||
Console.WriteLine("a % b = {0}", a % b); |
|||
} |
|||
}</csharp> |
|||
} |
|||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
<lisp>(defun arithmetic () |
|||
(let ((a (read *query-io*)) (b (read *query-io*))) |
|||
(format t "a + b = ~a~%" (+ a b)) |
|||
(format t "a - b = ~a~%" (- a b)) |
|||
(format t "a * b = ~a~%" (* a b)) |
|||
(format t "a / b = ~a~%" (/ a b)) |
|||
(format t "a % b = ~a~%" (mod a b))))</lisp> |
|||
=={{header|D}}== |
=={{header|D}}== |
||
<d>import std.stdio, std.string; |
|||
void main() |
|||
{ |
|||
auto a = readln().atoi(), b = readln().atoi(); |
|||
writefln("a + b = ", a+b); |
|||
writefln("a - b = ", a-b); |
|||
writefln("a * b = ", a*b); |
|||
writefln("a / b = ", a/b); |
|||
writefln("a % b = ", a%b); |
|||
}</d> |
|||
} |
|||
=={{header|DOS Batch File}}== |
=={{header|DOS Batch File}}== |
||
<pre>@set /P A=Enter 1st Number : |
<pre>@set /P A=Enter 1st Number : |
||
Line 168: | Line 169: | ||
main = do |
main = do |
||
a <- readLn :: IO Integer |
|||
b <- readLn :: IO Integer |
|||
let |
|||
a :: Integer |
|||
a = read as |
|||
b :: Integer |
|||
b = read bs |
|||
putStrLn $ "a + b = " ++ show (a + b) |
putStrLn $ "a + b = " ++ show (a + b) |
||
putStrLn $ "a - b = " ++ show (a - b) |
putStrLn $ "a - b = " ++ show (a - b) |
||
Line 188: | Line 184: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
<java>import java.util.Scanner; |
|||
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));//integer division truncates decimal places |
|||
System.out.println("remainder of a / b = " + (a % b)); |
|||
}</java> |
|||
} |
|||
=={{header|Logo}}== |
=={{header|Logo}}== |
||
Line 239: | Line 235: | ||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
<ocaml>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); |
|||
Printf.printf "a mod b = %d\n" (a mod b)</ocaml> |
|||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
Line 265: | Line 261: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{works with|Perl|5.x}} |
{{works with|Perl|5.x}} |
||
<perl>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" |
|||
;</perl> |
|||
=={{header|Pop11}}== |
=={{header|Pop11}}== |
||
Line 292: | Line 288: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
<python>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. |
|||
print "Remainder: %d" % (x % y) |
|||
## Only used to keep the display up when the program ends |
|||
raw_input( )</python> |
|||
=={{header|Raven}}== |
=={{header|Raven}}== |
||
Line 317: | Line 313: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
<ruby>puts 'Enter x and y' |
|||
x=gets.to_i |
|||
y=gets.to_i |
|||
puts "Sum: #{x+y}", |
|||
"Difference: #{x-y}", |
|||
"Product: #{x*y}", |
|||
"Quotient: #{x/y}", |
|||
"Remainder: #{x%y}"</ruby> |
|||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
<scheme>(define (arithmetic x y) |
|||
(for-each (lambda (op) |
|||
(write (list op x y)) |
|||
(display " => ") |
|||
(write ((eval op) x y)) |
|||
(write-char #\newline)) |
|||
'(+ - * / quotient remainder modulo max min gcd lcm))) |
|||
(arithmetic 8 12)</scheme> |
|||
prints this: |
prints this: |