3.2 C
New York
Thursday, December 1, 2022

How to Calculate the Factorial of a Number – MUO – MakeUseOf

It’s easy to manually compute a factorial, but how do you do so programatically, in the cleanest way possible?
The factorial of a number is an important mathematical concept. You can use it to perform permutations and combinations, write exponential and logarithmic expressions, and calculate probability.
You use it to find the number of different ways you can design a seating arrangement, or choose t-shirts for your vacation to the Maldives. But how can you calculate the factorial of a number?
The factorial of a positive number is the product of all positive integers less than or equal to the value of the number itself. 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 * 3 * 2 * 1 = 120
Another way to visualize it is:
5! = 5 * 4! where 4! = 4 * 3!, 3! = 3 * 2! and so on until you get 1! = 1 * 0! which is 1.
You will use this concept to build our factorial program using a popular concept called 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.
You can use recursion to solve appropriate problems in three easy steps:
You can use this concept to find the sum of n natural numbers, calculate GCD, LCM, the Fibonacci series, and check prime numbers.
This is how you use recursion and write the pseudo code to build your program in any language. With different languages, the syntax and execution change but the logic remains intact.
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 the main function. Here's how you can build the factorial program using recursion in C language:
You can find the entire source code of the factorial program using recursion in C, Java, and Python in this GitHub repository.
Java is a compiled programming language and is platform-independent. You store all the code inside a class and the execution begins from the main function. It is case-sensitive and syntax strict. The code is a bit longer but faster compared to Python. Here's how you can build the factorial program using recursion in Java:
Writing code in Python is super easy and fun. As it's an interpreted platform-independent language, you don't have to declare the data type of variables. You also avoid having to declare classes and import libraries for such a simple program. The playground is ready for you to start coding.
The syntax is easier, with a small code length but takes a little more time to execute than the other languages. Here's how you can build the factorial program using recursion in Python:
Recursion is an effective way of solving problems. It is the crux of Artificial Intelligence and has real-world uses in puzzle games such as chess or Sudoku.
It’s also a powerful method for sorting data structures such as Tree or sorting algorithms such as Quick sort and Merge sort. You might also use recursion in searching algorithms like Binary search, mathematical expressions such as the Fibonacci series, and more.
Sai Ashish is a Full Stack Developer with industry experience building websites and web applications. He loves to build innovative products and write insightful articles on programming.
Join our newsletter for tech tips, reviews, free ebooks, and exclusive deals!


Related Articles


Please enter your comment!
Please enter your name here

Latest Articles