Prime Number Calculator
0
How to calculate the factorial of a number
© Provided by MUO
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.
With it, you'll discover a number of different ways you can design seating arrangements or choose t-shirts for your Maldives holiday. But how can you calculate the factorial of a number?
What is 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 point (!) indicates the factorial of the number. You represent the factorial of five as 5! and calculate it as:
5! = 5 * 4 * 3 * 2 * 1 = 120
Another way to think of 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.
What is recursion?
Recursion is the process in which a function calls itself. One of the main advantages of this process is that it breaks a larger problem into smaller pieces. This makes it easier to solve the problem.
You can use recursion to solve the relevant problems in three easy steps:
Find the base case: If a function always calls itself, the process will be infinite. To avoid this, define a base case that becomes the logical stopping point for your function. For example, in a factorial program, stop the calculation at zero. This becomes the base case of the problem.
Find the relationship between the problem and the subproblems: Break down the larger problem into subproblems. For example, the problem is to find the factorial of five. Suppose you have the answer to the factorial of four, which is 24. How do you get the factorial of five using 24? By multiplying it by five. This is the relationship between a problem and a subproblem.
Generalize the relation found in step 2: Now that you have the relation, generalize it in terms of n. So the factorial of n is the product of n and the factorial of n-1.
You can use this concept to find the sum of n natural numbers, to calculate GCD, LCM, Fibonacci series, and to check prime numbers.
Pseudocode for factorial function using recursion
This is how you use recursion and write pseudo code to build your program in any language. For different languages, the syntax and implementation change, but the logic remains intact.
function Fact(n)
If n == 0 then // base case
Return 1
Return n * Call Fact(n - 1) // generalized relation
Factor program in C
C was the first high-level, platform-independent programming language. It has a strict syntax, is case sensitive and runs code at the fastest speed. It is a procedural programming language and therefore you declare any function above the main function. Here's how you can create a factorial program using recursion in C:
The full source code of a factorial program using recursion in C, Java, and Python can be found in this GitHub repository.
Import a standard input output header file to display the output on the screen. #include
Define a function fact and take an integer n as an argument. int fact(int n) {
Write the base case of the function using the if statement and check its equality with ==. If n is zero, return one.
if (n == 0)
return 1;
Write the generalized equation and return the product of n with the function call of subproblem n-1.
return n * fact(n - 1);
}
Declare a main function and initialize an integer variable to hold the number whose factorial you want to find.
int main() {
int num = 5;
Display the factorial of a number using the printf() function. %d is a decimal format specifier. Using each of the format specifiers, replace it with the number whose factorial you want to find, and call the function to get the result.
printf("Factorial %d is %d", num, fact(num));
return 0;
}
Factor program in Java
Java is a compiled programming language and is platform independent. You put all the code in a class and the execution starts from the main function. It is case sensitive and the syntax is strict. The code is slightly longer but faster compared to Python. Here's how you can create a factorial program using recursion in Java:
Define the main class. class Main {
Define a static function with return type int that accepts a variable n of integer type. You have declared a static method as the main method in Java is also declared as static. Additionally, you cannot call a non-static method from a static instance. static int fact(int n) {
Write the base case of the function using the if statement and check its equality with ==. If n is zero, return one.
if (n == 0)
return 1;
Write the generalized equation and return the product of n with the function call of subproblem n-1.
return n * fact(n - 1);
}
Declare a main function in Java. Declare the access modifier as public so that it can be accessed by all other classes and methods. You declare the main function as static so that the compiler can call it without instantiating the class. The return type is void and accepts String arguments. Store the number whose factorial you want to find.
public static void main(String[] args) {
int num = 5;
To display the factorial of a number, use the println() method, an instance of the PrintStream class, defined in the System class.
System.out.println("Factorial of " + num + " is " + fact(num));
}
}
Factor program in Python
Writing code in Python is super easy and fun. Since this is a platform-independent interpreted language, you do not need to declare the data type of the 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 simpler, with little code length, but it takes a bit longer to execute than other languages. Here's how you can create a factorial program using recursion in Python:
Define the function fact that you accept as argument n. def fact(n):
Write the base case of the function using the if statement and check its equality with ==. If n is zero, return one.
if n == 0:
return 1
Write the generalized equation and return the product of n with the function call of subproblem n-1. return n * fact(n-1)
Save the number whose factorial you want to find and display it using the printout.
number = 5;
print("Factorial of", num, "is", fact(num))
There are many applications of recursion
Recursion is an efficient way to solve problems. It is the core of artificial intelligence and has real uses in puzzle games like chess or sudoku.
It is also a powerful method for sorting data structures such as a tree or sorting algorithms such as quicksort and mergesort. You can also use recursion in search algorithms like binary search, mathematical expressions, etc


If you have any doubts, Please let me know