Code Scrappers


Permutation and Combination C program


                    #include<stdio.h>
                    #include<stdlib.h>

                    int fact(int);

                    void main()

                    {
                            int n, r, nCr, nPr;

                            printf("enter the value of n\n");

                            scanf("%d",&n);

                            printf("enter the value of r\n");

                            scanf("%d",&r);

                            if(r > n)
                            {
                                    printf("not possible\n");
                            }
                            else
                            {
                                    nCr = fact(n)/ (fact(r)*fact(n-r));

                                    nPr = fact(n)/ fact(n-r);

                                    printf("\nnPr = %d",nPr);

                                    printf("\nnCr = %d",nCr);
                            }
                    }

                    int fact(int n)

                    {
                            if(n >= 1)
                            {
                                    return n*fact(n-1);
                            }
                            else
                            {
                                    return 1;
                            }
                    }
                



Output

          enter the value of n
          8
          enter the value of r
          3

          nPr = 336
          nCr = 56