In: Computer Science
Module/Week 2 ASSIGNMENT (INPUT/OUTPUT)
The number of permutations of a set of n items taken r at a time is
given by the following
formulan!/r !(n- r )!: where n! is the factorial of n, r! is the
factorial of r, and (n-r)! is the
factorial of the result of n-r. The factorial of a number n can be
solved using the following
formula: n!=e-n nn √ 2πn.
If there are 18 people in your class and you want to divide the
class into programming teams of 3
members, you can compute the number of different teams that can be
arranged using this formula
(n!/r !(n- r )!).
Write a C++ program that determines the number of potential team
arrangements. You will need
to use the double type for this computation. Use the Lab Template
you set-up last week, proper
formatting, and appropriate comments in your code. The output must
be labeled clearly and
formatted neatly.
Submit C++ Programming Assignment 2 by 11:59 p.m. (ET) on Monday of
Module/Week 2
Hey , I dont know about lab template if you want to give please comment below
#include<bits/stdc++.h>
#include<numeric>
using namespace std;
int main()
{
double n,r,num;
cout<<"Enter the total students and number of children in one group"<<endl;
cin>>n>>r; // taking input total student and number of children in one team
double v=n-r; // declaring v=n-r
double ans;
for(int i=n-1;i>=1;i--)
{
n*=i; // finding factorial n! like n*(n-1)*(n-2)*...*1
}
for(int i=r-1;i>=1;i--)
{
r*=i; // finding factorial r! like r*(r-1)*(r-2)*...*1
}
for(int i=v-1;i>=1;i--)
{
v*=i;// finding factorial v! like v*(v-1)*(v-2)*...*1
}
// our formula is n!/(r!(v)!) where v=n-r
ans=n/r;
ans=ans/v;
cout<<"NUMBER OF ARRANGEMENTS "<<ans<<endl;
}
This is the basic code to do it. You can also find factorial by recursion if you want i can do this question by recursion.
Below is the screenshot of above code.
Please upvote
If face any problem comment below.