<?php // Formula: n! = n*(n-1)*(n-2)*(n-3)...3.2.1 and zero factorial is defined as one i.e. 0! = 1. // Method 1. Using for loop $fact = 1; $number = 5; for ($i = 1; $i <= $number; $i++) $fact = $fact * $i; echo $fact; //---------------------------------- // Method 2: Using recursive method function factorial($n) { if ($n == 0) return 1; else return($n * factorial($n - 1)); } $number=6; echo factorial($number); ?>
Wednesday, 19 November 2014
Simple Factorial Program in PHP
The factorial of a number is the product of all integers up to and including that number, so the factorial of 4 is 4*3*2*1= 24.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment