0

a function that makes permutations in c

#include <stdio.h> int perm(int x, int y); int main() { int perm(int x, int y){ int pro1=1, pro2=1, prrr; for(int i=1; i<=x; i++){ pro1 = pro1 * i; } x=pro1; for(int i=1; i<=y; i++){ pro2 = pro2 * i; } y=pro2; prrr = x/(x-y); return(prrr); } int x=10, y=2, permutation; permutation = perm(x , y); printf("%d", permutation); } I have used this code to compute permutations in c but it always prints out 1 . I don't understand what the problem is .

23rd Sep 2021, 1:18 AM
ahmed sallam
ahmed sallam - avatar
2 odpowiedzi
+ 2
If you are calculating nPr formula you made a small mistake. The actual formula is n! / (n-r)! What you did is n! / (n!-r!). I have changed something in the code check it out #include <stdio.h> int perm(int x, int y); int main() { int perm(int x, int y){ y=x-y; int pro1=1, pro2=1, prrr; for(int i=1; i<=x; i++){ pro1 = pro1 * i; } x=pro1; for(int i=1; i<=y; i++){ pro2 = pro2 * i; } y=pro2; prrr = x/y; return(prrr); } int x=10, y=2, permutation; permutation = perm(x , y); printf("%d", permutation); } I added y=y-x to find n-r Also prrr=x/y;
23rd Sep 2021, 2:40 AM
Arun Ruban SJ
Arun Ruban SJ - avatar
0
What output do you expect?
23rd Sep 2021, 1:56 AM
Slick
Slick - avatar