In Mathematics, the factorial of n is denoted by **n!** and calculated by the product of integer numbers from 1 to n.

For instance:

In this challenge you will write a program using Little Man Computer to ask the user to enter a positive number. In return the program will output the factorial of this number.

Before completing this challenge, we recommend you to have a go at:

#### Multiplication using LMC

While checking the Little Man Computer Instruction Set (See table at the bottom of this post), you will have noticed that, through there are two instructions for adding (ADD) and subtracting (SUB) numbers, there is no instruction for multiplying two numbers.

The solution to overcome this, is two consider a multiplication as a series of additions. For instance:

*5 * 4 = 5 + 5 + 5 + 5*

Find out more: Little Man Computer: Multiplication

#### Iteration using LMC

To apply an iterative approach for this challenge we will use branching options: Little Man Computer: 5 + 4 + 3 + 2+ 1.

This challenge will add a level of complexity as we will be using a nested loop approach: one loop to list all the multiplications to be performed (e.g. *4! = 4 * 3 * 2 * 1*) and one loop for implementing each multiplication as a series of additions. (e.g. *4 * 3 = 4 + 4 + 4*)

#### LMC Simulators

#### Solution

This solution also caters for the fact that

*0! = 1*. This is the purpose of line 3, 34,35 and 36.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
inp sta final brz oneval sub one sta iteration sta counter lda final sta num mult lda iteration brz end sub one brz end lda final add num sta final lda counter sub one sta counter sub one brz next bra mult next lda final sta num lda iteration sub one sta iteration sta counter sub one brz end bra mult end lda final out hlt oneval lda one out hlt final dat 0 counter dat 0 one dat 1 iteration dat 0 num dat 0 |

*© Solution by Howard C.*

#### LMC Instruction Set

*Note that in the following table “xx” refers to a memory address (aka mailbox) in the RAM. The online LMC simulator has 100 different mailboxes in the RAM ranging from 00 to 99.*

Mnemonic |
Name |
Description |
Op Code |

INP | INPUT | Retrieve user input and stores it in the accumulator. | 901 |

OUT | OUTPUT | Output the value stored in the accumulator. | 902 |

LDA | LOAD | Load the Accumulator with the contents of the memory address given. | 5xx |

STA | STORE | Store the value in the Accumulator in the memory address given. | 3xx |

ADD | ADD | Add the contents of the memory address to the Accumulator | 1xx |

SUB | SUBTRACT | Subtract the contents of the memory address from the Accumulator | 2xx |

BRP | BRANCH IF POSITIVE | Branch/Jump to the address given if the Accumulator is zero or positive. | 8xx |

BRZ | BRANCH IF ZERO | Branch/Jump to the address given if the Accumulator is zero. | 7xx |

BRA | BRANCH ALWAYS | Branch/Jump to the address given. | 6xx |

HLT | HALT | Stop the code | 000 |

DAT | DATA LOCATION | Used to associate a label to a free memory address. An optional value can also be used to be stored at the memory address. |