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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/ControlFlowGroup(["`Control Flow`"]) c(("`C`")) -.-> c/PointersandMemoryGroup(["`Pointers and Memory`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/comments("`Comments`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/data_types("`Data Types`") c/BasicsGroup -.-> c/operators("`Operators`") c/ControlFlowGroup -.-> c/if_else("`If...Else`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/PointersandMemoryGroup -.-> c/memory_address("`Memory Address`") c/PointersandMemoryGroup -.-> c/pointers("`Pointers`") c/FunctionsGroup -.-> c/function_parameters("`Function Parameters`") c/FunctionsGroup -.-> c/function_declaration("`Function Declaration`") c/FunctionsGroup -.-> c/recursion("`Recursion`") subgraph Lab Skills c/output -.-> lab-123209{{"`Calculate Permutation and Combination in C Language`"}} c/comments -.-> lab-123209{{"`Calculate Permutation and Combination in C Language`"}} c/variables -.-> lab-123209{{"`Calculate Permutation and Combination in C Language`"}} c/data_types -.-> lab-123209{{"`Calculate Permutation and Combination in C Language`"}} c/operators -.-> lab-123209{{"`Calculate Permutation and Combination in C Language`"}} c/if_else -.-> lab-123209{{"`Calculate Permutation and Combination in C Language`"}} c/user_input -.-> lab-123209{{"`Calculate Permutation and Combination in C Language`"}} c/memory_address -.-> lab-123209{{"`Calculate Permutation and Combination in C Language`"}} c/pointers -.-> lab-123209{{"`Calculate Permutation and Combination in C Language`"}} c/function_parameters -.-> lab-123209{{"`Calculate Permutation and Combination in C Language`"}} c/function_declaration -.-> lab-123209{{"`Calculate Permutation and Combination in C Language`"}} c/recursion -.-> lab-123209{{"`Calculate Permutation and Combination in C Language`"}} end

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