Permutations In Which N People Can Occupy R Seats In A Classroom | C++ Program
Permutations in which n people can occupy r seats in a classroom:-
C programming helps in identifying the r number of seats that can be occupied by n number of people. Such a program is known as the possible permutations. Here, We need to write a code to find all the possible permutations in which n people can occupy r number of seats in a classroom/theater.
N students are looking to find r seats in a classroom. Some of the seats are already occupied and only a few can accommodate in the classroom. The available seats are assumed as r and n number of people are looking to accommodate within the room.
Algorithm
Step 1: Start
Step 2: Ask the user to insert n number of people and the number of seats as r.
Step 3: Calculate permutation, p(n,r).
Step 4: Enter the program to calculate permutation P(n,r) = n! / (n-r)!
Step 5: Print the calculated result.
Step 6: Stop
C++ Code:-
#include <iostream>
using namespace std;
// Program to find the factorial of the number
int factorial (long int x)
{
long int fact=1,i;
for(i=1;i<=x;i++)
{
fact=fact*i;
}
return fact;
}
int main()
{
long int n,r,permutation,temp;
long int numerator, denominator;
// Insert the num of people
cout<<"\nEnter the number of persons : ";
cin>>n;
// Insert the num of seats
cout<<"\nEnter the number of seats available : ";
cin>>r;
// Base condition
// Swap n and r when n is less than r
if(n < r)
{
temp=n;
n=r;
r=temp;
}
numerator=factorial(n);
denominator=factorial(n-r);
permutation=numerator/denominator;
cout<<"\nNum of ways people can be seated : ";
cout<<permutation;
}Enter the number of persons : 3
Enter the number of seats available : 2
Num of ways people can be seated : 6
