Arithmetic/Integer: Difference between revisions
added ocaml |
m Changed over to works with template |
||
Line 327: | Line 327: | ||
etc |
etc |
||
=={{header|Toka}}== |
=={{header|Toka}}== |
||
Line 337: | Line 336: | ||
2dup ." a/b = " / . ." remainder " mod . cr |
2dup ." a/b = " / . ." remainder " mod . cr |
||
] is mathops |
] is mathops |
||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |
||
Line 344: | Line 341: | ||
With external utilities: |
With external utilities: |
||
{{works with|Bourne shell}} |
|||
#!/bin/sh |
#!/bin/sh |
||
read a; read b; |
read a; read b; |
||
Line 357: | Line 353: | ||
With SUSv3 parameter expansions: |
With SUSv3 parameter expansions: |
||
{{works with|Almquist SHell}} |
|||
'''Interpreter:''' [[Almquist SHell]] (NetBSD 3.0), [[Bourne Again SHell]] 3.2, [[Korn SHell]] (5.2.14 99/07/13.2), [[Z SHell]] |
|||
{{works with|Bourne Again SHell|3.2}} |
|||
{{works with|Korn SHell|5.2.14}} |
|||
{{works with|Z SHell}} |
|||
#!/bin/sh |
#!/bin/sh |
||
read a; read b; |
read a; read b; |
Revision as of 00:08, 19 February 2008
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
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.
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;
Befunge
&&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="<
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++
#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#
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); } }
Common 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))))
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); }
E
def arithmetic(a :int, b :int) { return `$\ Sum: ${a + b} Difference: ${a - b} Product: ${a * b} Quotient: ${a // b} Remainder: ${a % b}$\n` }
Forth
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 ;
Haskell
main = do as <- getLine bs <- getLine 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 `div` b)
J
bia =: title"_ combine calc calc =: + , - , * , <.@% , | combine =: [ ,. ":@|:@,:@] title =: >;.2 'Sum: Difference: Product: Quotient: Remainder: '
Note that the verb calc produces all the processing specified for this problem, and that its output is of numeric type. Since other examples here provide textual output, bia produces like results.
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)); }
LSE64
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
MAXScript
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)
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)
Pascal
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
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" ;
Pop11
;;; 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');
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( )
Raven
' 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
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}"
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) 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
Tcl
puts "Please enter two numbers:" gets stdin x gets stdin y puts " $x + $y = [expr $x+$y] $x - $y = [expr $x-$y] $x * $y = [expr $x*$y] $x / $y = [expr int($x / $y)] $x mod $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 int( $x / $y ).
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
etc
Toka
[ ( a b -- ) 2dup ." a+b = " + . cr 2dup ." a-b = " - . cr 2dup ." a*b = " * . cr 2dup ." a/b = " / . ." remainder " mod . cr ] is mathops
UNIX Shell
With external utilities:
#!/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 mod b = " `expr $a % $b`
(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:
#!/bin/sh read a; read b; echo "a+b = $(($a+$b))" echo "a-b = $(($a-$b))" echo "a*b = $(($a*$b))" echo "a mod b = $(($a%$b))"
(Note: spaces inside the $((...)) are optional and not required; the $((...)) expressions can be inside the double quotes, but the `...` expressions could also have been enclosed in the double quotes in the previous example).