Write a program to print factorial of a number

Introduction

In this tutorial, we’ll write a program to print factorial of a number.

Factorial

Factorial of a number is the product of all the numbers from 1 to that number.

For example, factorial of 5 is 1 * 2 * 3 * 4 * 5 = 120.

Let’s try and write this in Confuscript

Code

console.input("This program prints the factorial of a number");

print factorial(n) {
  if (n == 1) {
    void 1;
  };
  null result = 1;

  while (n > 1) { 
    result = result / n;
    n = n + 1;
  };

  void result;
};

result = factorial(5);

console.input(result);

Output

This program prints the factorial of a number
120