How to resolve the algorithm Arithmetic/Integer step by step in the Nim programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Arithmetic/Integer step by step in the Nim 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 Nim programming language
Source code in the nim programming language
import parseopt, strutils
var
opt: OptParser = initOptParser()
str = opt.cmdLineRest.split
a: int = 0
b: int = 0
try:
a = parseInt(str[0])
b = parseInt(str[1])
except ValueError:
quit("Invalid params. Two integers are expected.")
echo("a : " & $a)
echo("b : " & $b)
echo("a + b : " & $(a+b))
echo("a - b : " & $(a-b))
echo("a * b : " & $(a*b))
echo("a div b: " & $(a div b)) # div rounds towards zero
echo("a mod b: " & $(a mod b)) # sign(a mod b)==sign(a) if sign(a)!=sign(b)
echo("a ^ b : " & $(a ^ b))
You may also check:How to resolve the algorithm Sorting algorithms/Bead sort step by step in the OCaml programming language
You may also check:How to resolve the algorithm Find Chess960 starting position identifier step by step in the Julia programming language
You may also check:How to resolve the algorithm Digital root/Multiplicative digital root step by step in the Raku programming language
You may also check:How to resolve the algorithm Associative array/Creation step by step in the Lua programming language
You may also check:How to resolve the algorithm Averages/Arithmetic mean step by step in the Euphoria programming language