Program to Print Factorial of a Number in PHP – Iterative & Recursive Approach

Write a program to print factorial of a number in PHP. In this tutorial, We are going to write a PHP code to print factorial of a number using iterative as well as recursive approach. Before solving this problem let’s understand what is factorial.

Program to Print Factorial of a Number in PHP

Program to Print Factorial of a Number in PHP

If you are not familiar with the concept of recursive and iterative approach. Then check my previous tutorial on recursion Vs iteration.

What is Factorial?

Factorial of a non-negative integer n (n!), is the product of all positive integers less than or equal to n.

For example – 5! = 5 * 4 * 3 * 2 * 1 = 120

Subscribe Our Tutorials

Get Latest Updates on Facebook

Fibonacci series program in PHP

Palindrome program in PHP

Program to Print Factorial of a Number in PHP using Iteration

Let’s write a program to calculate factorial of a number using an iterative approach.

PHP Program to Print Factorial of a Number using Recursion

We have learned, how to calculate factorial of a number using iteration. Let’s write a program to calculate factorial of a number using recursion.

Recursion concept with an example

Recursion vs iteration

Explanation

This is how this program is executed.

MCQ on recursion for practice

We can also do the same thing by creating a class.Let’s create a class to calculate & print factorial of a number.

PHP Class to Calculate & Print Factorial of a Number

About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.
Tagged , , . Bookmark the permalink.