C Program to Find nPr

This is a C Program to calculate the value of nPr.

Problem Description

This C Program calculates the value of nPr.

Problem Solution

Here we need to find all possible rearrangement of the element i.e all the possible permutation value. A permutation is a re-arrangement of elements of a set. Any duplications of the collected elements in different orders are allowed. A permutation therefore tends to be a large number.

Program/Source Code

Here is source code of the C program to calculate the Value of nPr. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C program to Calculate the Value of nPr
 */
#include <stdio.h>
 
void main(void)
{
   printf("%d\n", fact(8));
   int n, r;
   printf("Enter value for n and r\n");
   scanf("%d%d", &n, &r);
   int npr = fact(n) / fact(n - r);
   printf("\n Permutation values is = %d", npr);
}
 
int fact(int x)
{
   if (x <= 1)
       return 1;
   return x * fact(x - 1);
}
Program Explanation

In this C program, we are reading the two integer values using ‘n’ and ‘r’ variables respectively. The fact() function is used to find all possible rearrangement of the elements. A permutation is a re-arrangement of elements of a set. Any duplication of the collected elements in different orders is allowed. A permutation therefore tends to be a large number.

advertisement
advertisement

If condition statement is used to check the integer value is less than or equal to 1. If the condition is true, then execute the statement and return the value as 1. Otherwise, if the condition is false then execute the else statement.

Compute the integer value with the next previous value i.e if the integer value is 3. Multiply the value as 3*2 then the resultant value 6 with 1 and return the value to ‘npr’ variable. Divide the value of ‘integer’ variable by fact(). Compute the difference of the value of ‘integer’ variable by the value of ‘r’ power variable. Print the value of nPr using the printf statement.

Runtime Test Cases
 
Output:
$ cc pgm13.c
$ a.out
40320
Enter value for n and r
5 4
 
Permutation values is = 120

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Mathematical Functions, go to C Programming Examples on Mathematical Functions. If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.