Arithmetic/Integer

From Rosetta Code
Revision as of 04:35, 14 August 2007 by rosettacode>Straz (added Scheme)
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

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;

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";
}

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 ;

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 $f = <>;
my $s = <>;

print map {"$_->[0]:\t$_->[1]\n"} (
    [sum                 => $f + $s],
    [difference          => $f - $s],
    [product             => $f * $s],
    ['integer quotient'  => int($f / $s)],
    [remainder           => $f % $s],
);

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');

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)

results

(+ 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 way differently 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