C program to Calculate the Value of 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.

advertisement
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

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.

advertisement

Here’s the list of Best Reference 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
advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn