C Program to Implement The Permutation

Permutation is an ordered arrangement of object. We can find in how many ways can n objects or a group of an objects can be arranged if r objects are selected at a time. This program uses factorial concept. You can refer to the following :

C Program to Implement The Permutation
C Program to Implement The Premutation

For example:- Let’s assume we have 9 balls. Then we can arrange 3 selected ball in P(9,3) = 504 i.e in 504 ways.
Permutation can be calculated by the formula : P(n,r) = n!/(n-r)! where n = number of available objects
and r = number of selected objects from the group.

Let’s understand with a algorithm as follows:-


ALGORITHM:-

step 1 : start
step 2 : Input Element in array
step 3 : Set mininum element to location of zero
step 4 : Search the mininum element in the array
step 5 : Swap the value with the Min Location
step 6 : Increments Min to point of the next Element
step 7 : Repeat this process until array in sorted
step 8 : Print the Sorted Element
step 9 : Stop the Excution

PROGRAM CODE:-

#include<stdio.h>
#include<conio.h>
int fact(int);
int main() {
    int p, r, n;
    printf("Enter Permutation Number for the pattern P(n,r)\n");
    printf("Enter n value: ");
    scanf("%d", &n);
    printf("\nEnter r value : ");
    scanf("%d", &r);
    p = (fact(n) / fact(n - r));     
    printf("\nFactorial is P(%d, %d) = %d", n, r, p);
    getch();
}
int fact(int c) {
    int f = 1;
    while(c > 0) {
         f = f * c;
         c--;
    }
    return f;
}

OUTPUT:-

Enter Permutation Number for the pattern P(n,r)
Enter n value: 4

Enter r value: 3
factorial of(4, 3) = 24

C Program To Implement the permutation of any number
Write a Program to Calculate the permutation