The factorial of a number is an important mathematical concept.
But how can you calculate the factorial of a number?
What Is the Factorial of a Number?
A number followed by an exclamation mark(!)
denotes the factorial of a number.
You represent the factorial of five as 5!
and calculate it as:
5!
= 5 * 4!
= 4 * 3!, 3!
= 3 * 2!
and so on until you get 1!
= 1 * 0!
You will use this concept to build our factorial program using a popular concept called recursion.
What Is Recursion?
Recursion is a process in which a function calls itself.
One of the main advantages of this process is that it breaks a bigger problem into smaller chunks.
This makes the problem easier to solve.
With different languages, the syntax and execution change but the logic remains intact.
Factorial Program in C
C was the first high-level, platform-independent programming language.
It has strict syntax, is case-sensitive, and executes code with the fastest speed.
It is a procedural programming language and hence you declare any function on top of themainfunction.
Factorial Program in Java
Java is a compiled programming language and is platform-independent.
You store all the code inside aclassand the execution begins from themainfunction.
It is case-sensitive and syntax strict.
The code is a bit longer but faster compared to Python.
You also avoid having to declare classes and import libraries for such a simple program.
The playground is ready for you to start coding.
It is the crux of Artificial Intelligence and has real-world uses in puzzle games such as chess or Sudoku.