C++ Program to Print nPr using While Loop
Write C++ Program to Print nPr using While Loop
// CPP Program to Print nPr using While Loop
#include <iostream>
using namespace std;
int fact(int n)
{
int f = 1;
int i = 1;
while(i <= n)
{
f = f * i;
i++;
}
return f;
}
int main()
{
int n, r;
cout << "Enter the value of n:--> ";
cin >> n;
cout << "Enter the value of r :--> ";
cin >> r;
int result = fact(n)/fact(n-r); //permutation formula
cout << "The value of nPr is " << result;
return 0;
}
Output:
Enter the value of n:--> 5
Enter the value of r :--> 3
The value of nPr is 60