Sunday, September 23, 2018

Print NCR

  • Problem Description

    Vidhya has a doubt in finding permutations and combinations. Can you help her to compute nCr of a given number n?
  • CODING ARENA
  • #include <stdio.h>
     
    int fact(int z);
     
    int main()
    {
        int n, r, ncr;
     
        scanf("%d%d", &n, &r);
        ncr = fact(n) / (fact(r) * fact(n - r));
        printf("%d", ncr);
      return 0;
    }
     
    int fact(int z)
    {
        int f = 1, i;
        if (z == 0)
        {
            return(f);
        }
        else
        {
            for (i = 1; i <= z; i++)
    {
                f = f * i;
    }
        }
        return(f);
    }
  • Test Case 1

    Input (stdin)
    2 1
    
    
    Expected Output
    2
  • Test Case 2

    Input (stdin)
    4 2
    
    
    Expected Output
    6

2 comments: