How to resolve the algorithm Arithmetic/Integer step by step in the Prolog programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Arithmetic/Integer step by step in the Prolog programming language

Table of Contents

Problem Statement

Get two integers from the user,   and then (for those two integers), display their:

Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, 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.

Bonus: Include an example of the integer divmod operator. For example: as in #Haskell, #Python and #ALGOL 68

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Arithmetic/Integer step by step in the Prolog programming language

Source code in the prolog programming language

print_expression_and_result(M, N, Operator) :-
    Expression =.. [Operator, M, N],
    Result is Expression,
    format('~w ~8|is ~d~n', [Expression, Result]).

arithmetic_integer :-
    read(M),
    read(N),
    maplist( print_expression_and_result(M, N), [+,-,*,//,rem,^] ).


?- arithmetic_integer.
|: 5.
|: 7.
5+7     is 12
5-7     is -2
5*7     is 35
5//7    is 0
5 rem 7 is 5
5^7     is 78125
true.


  

You may also check:How to resolve the algorithm Sum of a series step by step in the Phix programming language
You may also check:How to resolve the algorithm Number reversal game step by step in the Forth programming language
You may also check:How to resolve the algorithm Random number generator (device) step by step in the Forth programming language
You may also check:How to resolve the algorithm Solve a Holy Knight's tour step by step in the Picat programming language
You may also check:How to resolve the algorithm User input/Graphical step by step in the Python programming language