How to resolve the algorithm Arithmetic/Integer step by step in the NewLISP programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Arithmetic/Integer step by step in the NewLISP 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 NewLISP programming language
Source code in the newlisp programming language
; integer.lsp
; oofoe 2012-01-17
(define (aski msg) (print msg) (int (read-line)))
(setq x (aski "Please type in an integer and press [enter]: "))
(setq y (aski "Please type in another integer : "))
; Note that +, -, *, / and % are all integer operations.
(println)
(println "Sum: " (+ x y))
(println "Difference: " (- x y))
(println "Product: " (* x y))
(println "Integer quotient (rounds to 0): " (/ x y))
(println "Remainder: " (setq r (% x y)))
(println "Remainder sign matches: "
(cond ((= (sgn r) (sgn x) (sgn y)) "both")
((= (sgn r) (sgn x)) "first")
((= (sgn r) (sgn y)) "second")))
(println)
(println "Exponentiation: " (pow x y))
(exit) ; NewLisp normally goes to listener after running script.
You may also check:How to resolve the algorithm Balanced ternary step by step in the Koka programming language
You may also check:How to resolve the algorithm Yin and yang step by step in the Lambdatalk programming language
You may also check:How to resolve the algorithm Generate lower case ASCII alphabet step by step in the VBScript programming language
You may also check:How to resolve the algorithm String case step by step in the REBOL programming language
You may also check:How to resolve the algorithm Greatest common divisor step by step in the Standard ML programming language