Calculate Combination using Iterative Method in PHP
In this approach, we use iterative method to calculate the combination of given number.
PHP
<?php function combination( $n , $r ) { if ( $n < $r ) { return "Invalid input" ; } $result = 1; for ( $i = 0; $i < $r ; $i ++) { $result *= ( $n - $i ); $result /= ( $i + 1); } return $result ; } // Driver code $n = 5; $r = 2; echo "Combination: " . combination( $n , $r ); ?> |
Combination: 10
PHP Program to Calculate Combination nCr
This article will show you how to Calculate the Combination nCr in PHP.
Combinations refer to the different ways in which a set of items can be selected without considering the order. In mathematics, the number of combinations of ‘n’ distinct items taken ‘r’ at a time is denoted as nCr. The formula for combinations is nCr = n! / (r! * (n-r)!), where ‘!’ denotes the factorial of a number.