Write a program to find Permutations in which n people can occupy r seats in a classroom.

12 December 2022

Write a program to find Permutations in which n people can occupy r seats in a classroom.

If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023

Description

Get the number of people and number seats available as the input from the user and find the possible seating arrangements.

Input

2

5

Output

20

C program

#include<stdio.h>

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,num,den;

printf("Enter the  available seat number: ");

scanf("%ld",&r);

printf("Enter the number of people");

scanf("%ld",&n);

if(n<r)

{

temp=n;

n=r;

r=temp;

}

num=fact(n);

den=fact(n-r);

p=num/den;

printf("Number of ways people can be seated is: %d",p);

}

C++ Program

#include <iostream>

using namespace std;

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,num,den;

cout<<"Enter the  available seat number: ";

cin>>r;

cout<<"Enter the number of people: ";

cin>>n;

if(n<r)

{

temp=n;

n=r;

r=temp;

}

num=fact(n);

den=fact(n-r);

p=num/den;

cout<<"Number of ways people can be seated is: "<<p;

}

Java Program

import java.util.*;

class Main

{

public static void main(String[] args)

{

int n, r, ways, fact1, fact2;

Scanner sc =  new Scanner(System.in);

System.out.println("Enter the Value of n and r");

n = sc.nextInt();

r = sc.nextInt();

fact1 = n;

for (int i = n - 1; i >= 1; i=i-1)

{

fact1 = fact1 * i;

}

int num;

num = n - r;

fact2 = num;

for (int i = num - 1; i >= 1; i=i-1)

{

fact2 = fact2 * i;

}

ways = fact1 / fact2;

System.out.println(ways);

}

}

Python Program

import math

n = int(input('Enter the number of people :'))

r = int(input('Enter the number of seats :'))

num = math.factorial(n)

den = math.factorial(n-r)

ways = num//den

print(str(ways))

If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023