Lets write a C Program to Find nCr Factorial for the user input values of n and r.
In this video tutorial we are showing iterative logic, recursive logic and how to write single line of code for getting factorial using recursion and ternary/conditional operator.
Page Contents
nCr = n! / (r! * (n-r)!);
The number of different, unordered combination of r objects from a set of n objects is called nCr Factorial of n and r.
Very Important Note: n value should always be greater than value of r. Because, for example, we can get 2 out of 6. But we can’t get 6 out of 2. i.e., the main set should always be greater than the subset.
Lets assume that there are 3 people in a park. Let their names be A, B and C. There are only 2 seats available to sit. So the possible ways for people to sit over the seat using combination are {AB, AC, BC}. Since nCr is unordered combination AB and BA are considered same, so we use only one of them in our set. So in our example, we have 3 people, so n = 3. And we have 2 seats available, so r = 2. Finally we get 3 objects/combination in a set, so nCr is 3, when n = 3 and r = 2.
Factorial Definition: Factorial of a positive integer n, denoted by n!, is the product of all positive integers less than or equal to n.
Important Note: By convention, Factorial of 0 is 1. i.e., 0! = 1.
Example: 5! = 5 x 4 x 3 x 2 x 1 which is equal to 120. i.e., 5! = 120.
#include<stdio.h> int factorial(int); int main() { int n, r; float ncr; printf("Enter a positive value for n and r\n"); scanf("%d%d", &n, &r); ncr = factorial(n) / ( factorial(r) * factorial(n - r) ); printf("\nnCr Factorial of %d and %d is %0.2f.\n", n, r, ncr); return 0; } int factorial(int num) { int fact = 1, count; for(count = 1; count <= num; count++) { fact = fact * count; } return(fact); }
Output 1:
Enter a positive value for n and r
3
2
nCr Factor of 3 and 2 is 3.00.
Output 2:
Enter a positive value for n and r
6
2
nCr Factor of 6 and 2 is 15.00.
#include<stdio.h> int factorial(int); int main() { int n, r; float ncr; printf("Enter a positive value for n and r\n"); scanf("%d%d", &n, &r); ncr = factorial(n) / ( factorial(r) * factorial(n - r) ); printf("\nnCr Factorial of %d and %d is %0.2f.\n", n, r, ncr); return 0; } int factorial(int num) { if(num) return(num * factorial(num-1)); else return 1; }
Output 1:
Enter a positive value for n and r
3
2
nCr Factor of 3 and 2 is 3.00.
Output 2:
Enter a positive value for n and r
6
2
nCr Factor of 6 and 2 is 15.00.
#include<stdio.h> int factorial(int); int main() { int n, r; float ncr; printf("Enter a positive value for n and r\n"); scanf("%d%d", &n, &r); ncr = factorial(n) / ( factorial(r) * factorial(n - r) ); printf("\nnCr Factorial of %d and %d is %0.2f.\n", n, r, ncr); return 0; } int factorial(int num) { return( (num != 0) ? num * factorial(num-1) : 1); }
Output 1:
Enter a positive value for n and r
3
2
nCr Factor of 3 and 2 is 3.00.
Output 2:
Enter a positive value for n and r
6
2
nCr Factor of 6 and 2 is 15.00.
We ask the user to input positive integer value for n and r. We then make use of nCr formula to get the nCr value.
To find Factorial logic, please visit the links present below in “Related Read” section.
Related Read:
C Program To Find Factorial of a Number using Function
C Program To Find Factorial of a Number using Recursion
We have separate short videos explaining the iterative logic, recursive logic to find factorial of a user input number.
For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List
For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert