This is a C Program to calculate the value of nPr.
This C Program calculates the value of nPr.
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.
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); }
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.
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.
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
- Check Computer Science Books
- Apply for C Internship
- Practice BCA MCQs
- Watch Advanced C Programming Videos
- Apply for Computer Science Internship