Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

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

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)!   =    720.

So, there are 720 ways in which 5 students can be arranged in 6 seats.

Program to find permutations

Algorithm:-

  1. User gives two inputs(number of people and seats).
  2. The inputs are stored in two int type variables say n(number of people) & r(number of seats).
  3. If the number is people are more than the number of seats then stop the program otherwise go to next step

                  if(r<n)

                     cout<<“Cannot adjust “<<n<<” people on “<<r<<” seats”;

                 return 0;

       4. Find all the possible arrangements using the formula of permutation

               int p=fact(r)/fact(r-n);

       5. Print the output. 

C++ Code:-

    
    //C++ Program
    //Permutations in which n people can occupy r seats
    #include<iostream>
    using namespace std;
    //function for factorial
    int factorial(int num)
    {
        int fact=1;
        for(int i=num;i>=1;i–)
            fact*=i;
        return fact;
    }
    //main program
    int main()
    {
        int n,r;
        cout<<“Enter number of people: “;
        //user input
        cin>>n;
        cout<<“Enter number of seats: “;
        //user input
        cin>>r;
        //if there are more people than seats
        if(r<n)
        {
            cout<<“Cannot adjust “<<n<<” people on “<<r<<” seats”;
            return 0;
        }
        //finding all possible arrangements of n people on r seats
        // by using formula of permutation
        int p = factorial(r)/factorial(rn);
        //printing output
        cout<<“Total arrangements: “<<p;
        return 0;
    }

    Output

    Enter number of people: 5
    Enter number of seats: 6
    Total arrangements: 720
coding (3)
  • Highest Common Factor(HCF): C | C++Java | Python
  • Lowest Common Multiple (LCM) : C | C++ | Java | Python
  • Greatest Common Divisor : C | C++ | Java | Python
  • Binary to Decimal to conversion : C | C++ | Java | Python
  • Binary to Octal conversion : C | C++ | Java  | Python
  • Decimal to Binary conversion: C | C++ | Java | Python
  • Decimal to octal Conversion: C | C++ | Java | Python
  • Octal to Binary conversion : C | C++ | Java | Python
  • Octal to Decimal conversion : C | C++ | Java | Python
  • Quadrants in which a given coordinate lies : C | C++ | Java | Python
  • Permutations in which n people can occupy r seats in a classroom : C | C++ | Java | Python
  • Maximum number of handshakes: C | C++ | Java | Python
  • Addition of two fractions: C | C++ | Java | Python
  • Replace all 0’s with 1 in a given integer : C | C++ | Java | Python
  • Can a number be expressed as a sum of two prime numbers : C | C++ | Java | Python
  • Count possible decoding of a given digit sequence : C | C++ | Java
  • Check whether a character is a vowel or consonant : C | C++ | Java | Python
  • Check whether a character is a alphabet or not : C | C++ | Java | Python
  • Calculate the area of a circle : C | C++ | Java | Python
  • Find the ASCII value of a character : C | C++ | Java | Python
  • Find the prime numbers between 1 to 100 : C | C++ | Java | Python
  • Calculate the number of digits in an integer : C | C++ | Java | Python
  • Convert digit/number to words : C | C++ | Java | Python
  • Counting number of days in a given month of a year: C | C++ | Java | Python
  • Finding Number of times x digit occurs in a given input : C | C++ | Java | Python
  • Finding number of integers which has exactly x divisors: C | C++ | Java | Python
  • Finding Roots of a quadratic equation : C | C++ | Java | Python