# Find all possible permutations in which n people can occupy r seats in a theater

Program to find all possible permutations in which n people can occupy r seats in a theatre is discussed here.

N friends are planning to go to a movie. One among them suggested few movies and all others started to discuss and finally they selected a movie. One among them quickly booked their tickets online, to their surprise they are unable to select their seats. All of them got confused. Then anyhow, decided to go to the movie. They rushed to reach the theater on time. Again they are surprised that no one was there in the theater. They are the only people about to watch the movie. There is r number of seats in which, n number persons should sit. In how many ways they can sit inside the theater.?

Given the number of people n and the number of seats r as input. The task is to find the different number of ways in which n number of people can be seated in those r number of seats.

For example,

Input:

Number of people: 5
Number of Rows: 3

Output:

The total number of ways in which n people can be seated in r seats = 60.

Calculation:

P(n,r)   =  P(5,3
= 5! / − )
= 5! / ( 5 − 3 )!
= 120 / 2
= 60

## Algorithm to find all possible permutations in which n people can occupy r seats in a theater

• Input the number of people n and the number of seats r from the user.
• Calculate Permutation, p(n,r).
• P(n,r) = n! / (n – r)!
• Display the result.

Program to find all possible permutations in which n people can occupy r seats in a theater is given below.

// C program to find all possible permutations in which n people can occupy r seats in a theater

#include<stdio.h>
// Function to find the factorial of the number
int fact(long int x)
{
long int f=1,i;
for(i=1;i<=x;i++)
{
f=f*i;
}
return f;
}

int main()
{
long int n,r,p,temp;
long int num,den;
// Enter the number of seats
printf(“\nEnter the number of seats available : “);
scanf(“%ld”,&r);
// Enter the number of people
printf(“\nEnter the number of persons : “);
scanf(“%ld”,&n);
// Base condition
// Swap n and r
if(n < r)
{
temp=n;
n=r;
r=temp;
}
num=fact(n);
den=fact(n-r);
p=num/den;
printf(“\nNumber of ways people can be seated : “);
printf(“%ld\n”,p);
}

// C++ program to find all possible permutations in which n people can occupy r seats in a theater

#include<iostream>
using namespace std;

// Function to find the factorial of the number
int fact(long int x)
{
long int f=1,i;
for(i=1;i<=x;i++)
{
f=f*i;
}
return f;
}

int main()
{
long int n,r,p,temp;
long int num,den;
// Enter the number of seats
cout << “\nEnter the number of seats available : “;
cin >> r;
// Enter the number of people
cout << “\nEnter the number of persons : “;
cin >> n;
// Base condition
// Swap n and r
if(n < r)
{
temp=n;
n=r;
r=temp;
}
num=fact(n);
den=fact(n-r);
p=num/den;
cout << “\nNumber of ways people can be seated : “;
cout << p << endl;
}

// Java program to find the number of times digit 3 occurs in each and every number from 0 to n

import java.util.*;
public class Main
{
static int fact(int x)
{
int f=1,i;
for(i=1;i<=x;i++)
{
f=f*i;
}
return f;
}

public static void main(String[] args)
{
int n,r,p,temp;
int num,den;
Scanner sc = new Scanner(System.in);

// Enter the number of seats
System.out.print(“\nEnter the number of seats available : “);
r = sc.nextInt();

// Enter the number of people
System.out.print(“\nEnter the number of persons : “);
n = sc.nextInt();

// Base condition
// Swap n and r
if(n < r)
{
temp=n;
n=r;
r=temp;
}
num=fact(n);
den=fact(n-r);
p=num/den;
System.out.print(“\nNumber of ways people can be seated : ” + p);
}
}

# Python program to find the number of times digit 3 occurs in each and every number from 0 to n
def fact(x):
f=1
for i in range(1,x+1):
f=f*i
return f

r = int(input(“Enter the number of seats available : “))
n = int(input(“Enter teh number of people : “))
if(n < r):
temp=n
n=r
r=temp
num=fact(n)
den=fact(n-r)
p=int(num/den)
print(“Number of ways in which people can be seated :”,p)

Output: Time complexity: O(n)