Permutations In Which N People Can Occupy R Seats In A Classroom | C Program
Which n people can occupy r seats in a classroom in C
Which n people can occupy r seats in a classroom in 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.
Problem Statement :
In a classroom some of the seats are already occupied by students and only a few seats are available in the classroom. The available seats are assumed as r and n number of students are looking for the seat. We need to find in how many different permutations n number of students can sit on r number of chairs.
Algorithm
- Input number of students in n
- Input number of seats in r
- Use permutation formula { factorial(n) / factorial(n-r) }
- Print Output
C code
//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); //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 possible arrangements: %d",p); return 0; }
Output
Enter number of people: 5 Enter number of seats: 9 Total possible arrangements: 15120
For similar question click on given button.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment
int main()
{
int fact=1,n,j,i,r,deno=1,num,result;
printf(“Enter the number of persons and Seats=”);
scanf(“%d %d”,&n,&r);
for(i=1;i0;j–)
{
deno=deno*j;
}
result=num/deno;
printf(“Number of ways people can be seated is %d”,result);
}
#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;
}
JAVA CODE
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
//scanner class object creation
Scanner sc = new Scanner(System.in);
System.out.print(“Enter number of seats : “);
int n = sc.nextInt();
System.out.print(“Enter number of people : “);
int r = sc.nextInt();
int fact = 1;
int fact2 = 1;
for(int i = 1 ; i <= n ; i++) {
fact = fact * i;
}
int diff = n – r;
for(int i = 1 ; i <= diff ; i++) {
fact2 = fact2 * i;
}
int total = fact / fact2;
System.out.print(+r+" people can sit in "+n+ " seats in "+total+" number of ways." );
sc.close();
}
}
easier implementation is possible
#include
int main()
{
int n,r,i;
long int ans=1;
scanf(“%d%d”,&n,&r);
for(i=n;i>=n-r+1;i–)
{ ans*=i;
}
printf(“%ld”,ans);
}
#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)
Thanks Bhoma for this answer in Python, and thank you for appreciating our work by actively commenting on our pages.