Write a C program to calculate nCr and nPr.




  • nCr = n! / r!(n-r)!  
  • nPr = n! / (n-r)! 
Solve for, nCr
 
 #include<stdio.h>
#include <math.h>

int fact(int n)
{
    int i, fact = 1;

    for (i = 1; i <= n; i++)
    {
        fact = fact * i;
    }

    return fact;
}


int main()
{

    int n, r, s;
    double nCr;    
    printf(" Enter the value of n = ");
    scanf("%d", &n);

    printf(" Enter the value of r = ");
    scanf("%d", &r);
    if (n < r)
    {
        printf(" n must be greater or Qual r "); 
        return 0;
    }

    s = n - r;
    
    nCr = (fact(n)) /( fact(r) * fact(s)  );   

    printf("\n    nCr = %.lf  \n  ", nCr);    

    return 0;
}



Solve for, nPr 
 
 
#include<stdio.h>
 #include <math.h>

int fact(int n)
{
    int i, fact = 1;

    for (i = 1; i <= n; i++)
    {
        fact = fact * i;
    }

    return fact;
}


int main()
{

    int n, r, s;
    double nPr;   

    printf(" Enter the value of n = ");
    scanf("%d", &n);

    printf(" Enter the value of r = ");
    scanf("%d", &r);

    if (n < r)
    {
        printf(" n must be greater or Qual r "); 
        return 0;
    }

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

    printf("\n    nPr = %.lf  \n  ", nPr);   

    return 0;
}

নাফিজ বারাকাহ্

Content Creator, Writer, Designer. Writes technology based articles, poems & Product reviews. Create gameplay videos on youtube, designs Logo, Poster also Typography as a Hobby.

Post a Comment

Thank you for sharing your valuable comment.

Previous Post Next Post