Home > Backend Development > C++ > C program to calculate nPr value?

C program to calculate nPr value?

WBOY
Release: 2023-08-26 09:45:17
forward
644 people have browsed it

C program to calculate nPr value?

Permutation and combination, nPr can also be expressed as P(n,r), which is a mathematical formula used to calculate the number of permutations. The formula of P(n,r) is n! / (n – r)!.

The number of permutations on a set of n elements is given by n!, where "!" represents factorial. The Chinese translation of

Input:n=5;r=4;
Output:120
Copy after login

Explanation

is:

Explanation

P(5, 4) = 5! / (5-4)! => 120 / 1 = 120
5!=1*2*3*4*5*=120
Copy after login

Example

The Chinese translation is:

Example

#include<iostream>
using namespace std;
long int fact(int x) {
   int i, f=1;
   for(i=2; i<=x; i++) {
      f=f*i;
   }
   return f;
}
int main() {
   int n, r;
   long int npr;
   n=5;
   r=4;
   npr=fact(n)/fact(n-r);
   printf("%d",npr);
}
Copy after login

The above is the detailed content of C program to calculate nPr value?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template