Write a C program to calculate the value of rPr

The  nPr termed as permutation. They are useful in finding possible permutation of the number in the sets of numbers. To calculate combinations, we will use the formula nPr = n! /  (n – r)! , where n represents the total number of items, and r represents the number of items being chosen at a time.

 

#include <stdio.h>

int fact(int n)
{
    int i;
    int res = 1;
    for (i = 2; i <= n; i++)
    {
        res = res * i;
    }
    return res;
}


int getnPr(int n, int r)
{
    return fact(n)/fact(n-r);
}


int main()
{
    int num, r;
    long nprValue;

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

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

    nprValue = getnPr(num, r);

    printf("%d C %d = %ld\n", num, r, nprValue);

    return 0;
}

Output:

Enter the value of num = 10
Enter the value of r = 4
10 P 4 = 5040

 

Leave a Reply

Your email address will not be published. Required fields are marked *