You're online now.

Hurray! you are online now.

Write a C program to calculate the permutations (nPr)

In mathematics, nPr (n Permute r) represents the number of ways to select and arrange r objects from a set of n distinct objects, where order matters and repetition is not allowed. The formula for nPr is given by: nPr = n! / (n-r)!, Where n! represents the factorial of n (i.e., the product of all positive integers up to n) and (n-r)! represents the factorial of the difference between n and r.

// Write a c program to calculate the Permutation nPr.
    #include<stdio.h>
    
    int factorial(int n){
        int fact = 1;
        for(int i = 1; i <= n; i++){
            fact *= i;
        }
        return fact;
    }
    
    int nPr(int n, int r){
        int nPr = factorial(n) / factorial(n - r);
        return nPr;
    }
    
    int main(){
        int n, r;
        printf("Enter the value of n : ");
        scanf("%d", &n);
        printf("Enter the value of r : ");
        scanf("%d", &r);
        int result = nPr(n, r);
        printf("nPr = %d", result);
        return 0;
    }
#include<stdio.h>

This line of code includes the standard input/output library, which provides the function for input and output operations.

// factorial function to calculate the factorial.
    int factorial(int n){
        int fact = 1;
        for(int i = 1; i <= n; i++){
            fact *= i;
        }
        return fact;
    }

This is a function called factorial that takes an integer n and returns the factorial of n. The for loop iterates from i = 1 to i = n and multiplies fact by i in each iteration. Finally, the fact is returned.

int nPr(int n, int r){
        int nPr = factorial(n) /factorial(n - r);
        return nCr;
    }

This is another function called nPr that takes two integers n and r, and returns the number of permutations of n things takes r at a time. It calculates the permutations using the formula nPr = n! / (n - r)!, where n! and (n - r)! are the factorials of n and n - r, respectively. Finally, it returns nPr.

// main function (entry point of every program).
    int main(){
        int n, r;
        printf("Enter the value of n : ");
        scanf("%d", &n);
        printf("Enter the value of r : ");
        scanf("%d", &r);
        int result = nPr(n, r);
        printf("nPr = %d", result);
        return 0;
    }

This is the main function where the program execution starts. It declares two integer variables n and r. Then it prompts the user to input the values of n and r. The scanf() function reads integer values from the console and assigns them to n and r. Then the nPr function is called with arguments n and r, and the result is printed to the console with a message “nPr = ” using the pritf() function. The return 0; statement at the end of the main function is used to indicate successful program execution.

Algorithm

  1. Include the standard input/output library.
  2. Define a function to calculate the factorial of a given integer n.
  3. Define a function to calculate the permutations nCr using the factorial function.
  4. In the main function, read input values for n and r using the scanf() function.
  5. Compute the permutations using the nPr function.
  6. Print the result to the console using printf() function.
  7. End the program.

Pseudocode

// pseudocode of nPr
    1. INCLUDE stdio.h
    2. FUNCTION factorial(n)
        1. SET fact = 1
        2. FOR i = 1 to n
            1. SET fact = fact * i
        3. RETURN fact
    3. FUNCTION nPr(n, r)
        1. SET nCr = factorial(n) / factorial(n - r)
        2. RETURN nCr
    4. FUNCTION main()
        1. DECLARE n, r, result AS INTEGER
        2. PRINT "Enter the value of n : "
        3. READ n FROM USER
        4. PRINT "Enter the value of r : "
        5. READ r FROM USER
        6. SET result = nPr(n, r)
        7. PRINT "nPr = ", result
        8. RETURN 0
🖤 0
Buy me coffee ☕

Comments

Oops!

No comments here...