PHP Program to Calculate Combination nCr Last Updated : 01 Aug, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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.What is nCr Formula?The nCr represents "n choose r," a concept in combinatorics that calculates the number of ways to select a group of items from a larger set without considering the order of selection. It is denoted mathematically as:nCr = n! / (r!(n – r)!)Where,n is the total number of items in the set,r is the number of items to be chosen, and! denotes factorial, which is the product of all positive integers from 1 to the given number.Calculate Combination using Factorial Method in PHPIn this approach, we use factorial method to calculate the Combination i.e. nCr. The formula to calculate the combination is nCr = n! / (r! * (n-r)!).Example: PHP <?php // Function to calculate factorial function factorial($n) { if ($n == 0 || $n == 1) { return 1; } else { return $n * factorial($n - 1); } } // Function to calculate combination function combination($n, $r) { if ($n < $r) { return "Invalid input"; } return factorial($n) / (factorial($r) * factorial($n - $r)); } // Driver code $n = 5; $r = 2; echo "Combination: " . combination($n, $r); ?> OutputCombination: 10 Calculate Combination using Iterative Method in PHPIn 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); ?> OutputCombination: 10 Comment More infoAdvertise with us Next Article PHP Program to Count set bits in an integer B blalverma92 Follow Improve Article Tags : PHP Geeks Premier League 2023 Similar Reads PHP Program to Count set bits in an integer Write an efficient program to count number of 1s in binary representation of an integer. Examples : Input : n = 6 Output : 2 Binary representation of 6 is 110 and has 2 set bits Input : n = 13 Output : 3 Binary representation of 11 is 1101 and has 3 set bits Recommended: Please solve it on âPRACTICE 2 min read PHP Program for nth Catalan Number Catalan numbers are defined as a mathematical sequence that consists of positive integers, which can be used to find the number of possibilities of various combinations.Catalan numbers occur in many interesting counting problems like the following. Count the number of expressions containing n pairs 5 min read PHP Program for Coin Change | DP-7 Write a PHP program for a given integer array of coins[ ] of size N representing different types of denominations and an integer sum, the task is to find the number of ways to make a sum by using different denominations. Examples: Input: sum = 4, coins[] = {1,2,3}, Output: 4Explanation: there are fo 6 min read Program to print multiplication table of any number in PHP In this article, we will see how to print the multiplication table of any given number using PHP. To make the multiplication table, first, we get a number input from the user and then use for loop to display the multiplication table. We use HTML and PHP to display the multiplication table. The HTML 1 min read Calculator in PHP using If-Else Statement PHP (Hypertext Preprocessor) is an open-source & server-side scripting language designed specifically for web development, where the scripts are executed on the server. It is free to download and use & can be easily embedded with HTML files. We can also write HTML codes in a PHP file. PHP ca 4 min read PHP array_combine() Function The array_combine() function is an inbuilt function in PHP which is used to combine two arrays and create a new array by using one array for keys and another array for values. That is all elements of one array will be the keys of new array and all elements of the second array will be the values of t 2 min read Like