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 ways 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.

Permutations In Which N People Can Occupy R Seats in C

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)

                     print : “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. 

Permutations in which n people can occupy r seats in aclassroom in C programming
Competitive Coding Techniques

Code for Permutations In Which N People can occupy r seats in C :


    //Permutations in which n people can occupy r seats
#include<stdio.h>
//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;
printf("Enter number of people: ");
//user input
scanf("%d",&n);
printf("Enter number of seats:");
//user input
scanf("%d", &r);
//if there are more people than seats
if(r<n)
{
printf("Cannot adjust %d people on %d seats", n, r);
return 0;
}
//finding all possible arrangements of n people on r seats
// by using formula of permutation

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

//printing output
printf("Total arrangements: %d",p);
return 0;
}

Output

Enter the num of persons: 5
Enter the number of seats available: 9
Num of ways people can be seated: 15120

3 comments on “Permutations In Which N People Can Occupy R Seats In A Classroom | C Program”


  • Abhishek

    #include
    int seat(int n, int r)
    {
    if (r == 1)
    {
    return n;
    }
    if (r > n)
    {
    return n * r;
    }
    while (r > 1)
    {
    r–;
    return n * seat(n – 1, r);
    }
    }
    int main()
    {
    int n, r, c;
    scanf(“%d %d”, &n, &r);
    c = seat(n, r);
    printf(“%d”, c);
    return 0;
    }


  • Bhoma

    #Very Simple Way
    def fact(x):
    if (x==0 or x==1):
    return 1
    else:
    return (x*fact(x-1))

    #Driver Code and apply metohd.
    n = int(input(‘enter no of peoples: ‘))
    r = int(input(‘enter no of seats: ‘))
    fact(n)
    fact(n-r)
    p = fact(n)//fact(n-r)
    print(“no of ways available for people are” , p)


    • HelpPrepInsta

      Thanks Bhoma for this answer in Python, and thank you for appreciating our work by actively commenting on our pages.