examples/tiny/samples/factorial.tiny
raw| 1 | /* Sample program in Tiny language – computes factorial |
| 2 | from https://a7medayman6.github.io/Tiny-Compiler/ |
| 3 | */ |
| 4 | int main() |
| 5 | { |
| 6 | int x; |
| 7 | read x; /*input an integer*/ |
| 8 | |
| 9 | if x > 0 /*don’t compute if x <= 0 */ |
| 10 | then |
| 11 | int fact := 1; |
| 12 | repeat |
| 13 | fact := fact * x; |
| 14 | x := x - 1; |
| 15 | until x = 0 |
| 16 | write fact; /*output factorial of x*/ |
| 17 | elseif x = 0 then |
| 18 | write 1; |
| 19 | else |
| 20 | write "Error: x must be >= 0"; |
| 21 | end |
| 22 | return 0; |
| 23 | } |