Calculate Permutation and Combination in C Language

CCBeginner
Practice Now

Introduction

Permutation and combination are two different mathematical concepts. Permutation is the arrangement of objects in a specific order. Combination is a selection of objects from a set without any regard to the order of the selected objects. In this lab, we will learn how to calculate permutation and combination using C language.

Write the main function

First, let's create a new file named main.c in the ~/project/ directory and write the initial code for including header files and the main function.

#include <stdio.h>

int main() {
    printf("Permutation and Combination in C Language\n\n");
    // Code for permutation and combination calculation will be added here
    return 0;
}

Write the factorial function

To calculate permutation and combination, we need to calculate the factorial of a number. Factorials can be calculated recursively. Add the following code outside the main function.

long factorial(int num) {
    if (num == 0) {
        return 1;
    }
    else {
        return num * factorial(num - 1);
    }
}

This function takes an integer as an argument and returns its factorial.

Write the function to calculate nCr

Add the following function outside the main function to calculate nCr.

long nCr(int n, int r) {
    if (n < r) {
        return -1;
    }
    else {
        return factorial(n) / (factorial(r) * factorial(n - r));
    }
}

This function takes two integers, n and r, as arguments and returns the nCr value. The nCr value is calculated by dividing the factorial of n by the product of factorial r and factorial n-r.

Write the function to calculate nPr

Add the following function outside the main function to calculate nPr.

long nPr(int n, int r) {
    if (n < r) {
        return -1;
    }
    else {
        return factorial(n) / factorial(n - r);
    }
}

This function takes two integers, n and r, as arguments and returns the nPr value. The nPr value is calculated by dividing the factorial of n by the factorial of n-r.

Write the program's driver code

Add the following code inside the main function to take inputs and display the results.

int main()
{
    int n, r;

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

    printf("nCr is %ld\n", nCr(n, r));
    printf("nPr is %ld\n", nPr(n, r));

    return 0;
}

This code takes the input values for n and r from the user and calls the nCr() and nPr() functions to calculate the respective values and then displays them.

Full Code

The final code looks like the following.

#include <stdio.h>

long factorial(int num) {
    if (num == 0) {
        return 1;
    }
    else {
        return num * factorial(num - 1);
    }
}

long nCr(int n, int r) {
    if (n < r) {
        return -1;
    }
    else {
        return factorial(n) / (factorial(r) * factorial(n - r));
    }
}

long nPr(int n, int r) {
    if (n < r) {
        return -1;
    }
    else {
        return factorial(n) / factorial(n - r);
    }
}

int main()
{
    int n, r;

    printf("Enter the value of n:");
    scanf("%d", &n);
    printf("\nEnter the value of r:");
    scanf("%d", &r);

    printf("nCr is %ld\n", nCr(n, r));
    printf("nPr is %ld\n", nPr(n, r));

    return 0;
}

Summary

In this lab, we learned how to calculate permutation and combination in C language. We wrote the functions to calculate factorial, nCr, and nPr. We also wrote the driver code for the program. By following the above steps, we can calculate permutation and combination for the user inputs.

Other C Tutorials you may like