From fa9765276f03aa4acaae386a367c41cef23f4aad Mon Sep 17 00:00:00 2001 From: Ayush Parikh Date: Wed, 26 May 2021 10:14:56 +0530 Subject: [PATCH] Create calc_nPr.c --- calc_nPr.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 calc_nPr.c diff --git a/calc_nPr.c b/calc_nPr.c new file mode 100644 index 0000000..7c71fae --- /dev/null +++ b/calc_nPr.c @@ -0,0 +1,42 @@ +#include + +void main() +{ + int n, r, npr_var; + + printf("Enter the value of n:"); + scanf("%d", &n); + printf("\nEnter the value of r:"); + scanf("%d", &r); + + /* nPr is also known as P(n,r), the formula is: + * P(n,r) = n! / (n - r)! For 0 <= r <= n. + */ + npr_var = fact(n) / fact(n - r); + printf("\nThe value of P(%d,%d) is: %d",n,r,npr_var); +} +// Function for calculating factorial +int fact(int num) +{ + int k = 1, i; + // factorial of 0 is 1 + if (num == 0) + { + return(k); + } + else + { + for (i = 1; i <= num; i++) + { + k = k * i; + } + } + return(k); +} +Output: + +Enter the value of n: +5 +Enter the value of r: +2 +The value of P(6,2) is: 30