Basics of Forth, pt. 2
Write a program that takes an integer, calculates the square and prints the result on the screen also print out a newline character \n after the result.
: square-calc ( n -- n*n )
depth 1 <
if
." Supply an integer and call square-calc again. "
else
." The square of the number you entered is "
dup * . cr
then ;
Write a program that first asks the user for an integer and after that, a floating-point number. Finally, the program prints both numbers on the screen. The floating-point number shall be printed with two decimal places of precision.
: int-float-print ( n n -- )
depth 2 <
if
." Supply an integer and a float then call int-float-print again. "
else
." You entered the decimal number, rounded to two decimal places: "
2 (f.n)
cr
." You entered the integer: "
.
cr
then ;
Write a program that prompts the user for an amount in Finnish markka and converts it to euro. Finally, the program prints the amount on the screen in euro with two decimal places of precision. The euro conversion factor is 5.94573.
: FIM-to-EUR ( n -- m )
depth 1 <
if
." Supply the amount of FIM to convert to EUR "
else
." FIM converted to euro: "
5,94573
f/
2 (f.n)
cr
then ;
Write a program that prompts the user for two integers and prints the sum, difference and product of the numbers on the screen.
: sum-dif-prod ( n m -- p q r )
depth 2 <
if
." Supply 2 integer "
else
over over over over
cr
." Sum : " + .
cr
." Diff : " - .
cr
." Prod : " * .
cr
then ;