C++ Program to find Permutations in which n people can occupy r seats in a classroom
Program to find Permutations in which n people can occupy r seats in a classroom
Here we will discuss how to find the number of ways N people can occupy R seats in a classroom in C++ programming language.
This is a problem specific program which deals with problems like in how many ways employees can be arranged in the seats available in a company or the same for the students in a school or college.
This problem can be easily solved by the use of the simple formula of permutations which is
nPr = n! / (n-r)!
Example: we have 5 students and the total number of seats are 6.
Permutations = 6P5
= 6! / (6-5)!
So, there are 720 ways in which 5 students can be arranged in 6 seats.
Let’s see how the code works:
- User gives two inputs(number of people and seats).
- The inputs are stored in two int type variables say n(number of people) & r(number of seats).
- If the number is people are more than the number of seats then stop the program otherwise go to next step
cout<<“Cannot adjust “<<n<<” people on “<<r<<” seats”;
- Find all the possible arrangements using the formula of permutation
- Print the output.
//Permutations in which n people can occupy r seats
using namespace std;
//function for factorial
int factorial(int num)
cout<<"Enter number of people: ";
cout<<"Enter number of seats: ";
//if there are more people than seats
cout<<"Cannot adjust "<<n<<" people on "<<r<<" seats";
//finding all possible arrangements of n people on r seats
// by using formula of permutation
int p = factorial(r)/factorial(r-n);
cout<<"Total arrangements: "<<p;
Enter number of people: 5
Enter number of seats: 6
Total arrangements: 720