Exercise 2: In probability theory, one problem that often arises is determining the number of ways in which p objects can be selected from n distinct objects without regard to the order in which they are selected. Such selections are called combinations. The number of combinations of p objects from a set with n objects is C (n, p) and is given by: п! C(n, p) = (n – p)! * p! Write a Python program which will calculate and display on the screen the number of possible combination C based on the values of n and p; where both n and p are positive integers less than 21 and n>p. Implement and use at least the following three functions: fact(...): A function when passed a positive integer value, will calculate and return the factorial of that number. comb(...): A function when passed n and p (n: total number of objects and p: number of objects taken at a time) will calculate and return the total number of possible combinations (using the formula above). It calls function fact(.…..) 12 main(...): reads and validates the values of n and p, calls function comb(…) and prints the result. Enter n and p each in range [0,20] and n>p: -8 10 Invalid input, try again Enter n and p each in range [0,20] and n>p: 5 -6 Invalid input, try again Enter n and p each in range [e,20] and n>p: 51 8 Invalid input, try again Enter n and p each in range [0,20] and n>p: 9 17 Invalid input, try again Enter n and p each in range [0,20] and n>p: 8 6 Number of combinations c(8,6) = 28 Figure 2. Exercise 2 Sample Run

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter16: Searching, Sorting And Vector Type
Section: Chapter Questions
Problem 1TF
icon
Related questions
Question

By using functions:

Please Write Python program design at the beginning of program:define the purpose, Input /output data and Algorithm

and please write the program testing ( for cases that could happen)

Exercise 2:
In probability theory, one problem that often arises is determining the number of ways in which p objects
can be selected from n distinct objects without regard to the order in which they are selected. Such selections
are called combinations. The number of combinations of p objects from a set with n objects is C (n, p) and
is given by:
п!
С(п, р)
(n – p)! * p!
Write a Python program which will calculate and display on the screen the number of possible combination
C based on the values of n and p; where both n and p are positive integers less than 21 and n > p.
Implement and use at least the following three functions:
fact(..): A function when passed a positive integer value, will calculate and return the factorial of
that number.
comb(...): A function when passed n and p (n: total number of objects and p: number of objects
taken at a time) will calculate and return the total number of possible combinations
(using the formula above). It calls function fact(.…..)
12
main(...): reads and validates the values of n and p, calls function comb(...) and prints the result.
Enter n and p each in range [0,20] and n>p: -8 10
Invalid input, try again
Enter n and p each in range [0,20] and n>p: 5 -6
Invalid input, try again
Enter n andp each in range [0,20] and n>p: 51 8
Invalid input, try again
Enter n and p each in range [0,20] and n>p: 9 17
Invalid input, try again
Enter n andp each in range [0,20] and n>p: 8 6
Number of combinations c(8,6) = 28
Figure 2. Exercise 2 Sample Run
Transcribed Image Text:Exercise 2: In probability theory, one problem that often arises is determining the number of ways in which p objects can be selected from n distinct objects without regard to the order in which they are selected. Such selections are called combinations. The number of combinations of p objects from a set with n objects is C (n, p) and is given by: п! С(п, р) (n – p)! * p! Write a Python program which will calculate and display on the screen the number of possible combination C based on the values of n and p; where both n and p are positive integers less than 21 and n > p. Implement and use at least the following three functions: fact(..): A function when passed a positive integer value, will calculate and return the factorial of that number. comb(...): A function when passed n and p (n: total number of objects and p: number of objects taken at a time) will calculate and return the total number of possible combinations (using the formula above). It calls function fact(.…..) 12 main(...): reads and validates the values of n and p, calls function comb(...) and prints the result. Enter n and p each in range [0,20] and n>p: -8 10 Invalid input, try again Enter n and p each in range [0,20] and n>p: 5 -6 Invalid input, try again Enter n andp each in range [0,20] and n>p: 51 8 Invalid input, try again Enter n and p each in range [0,20] and n>p: 9 17 Invalid input, try again Enter n andp each in range [0,20] and n>p: 8 6 Number of combinations c(8,6) = 28 Figure 2. Exercise 2 Sample Run
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Knowledge Booster
Bare Bones Programming Language
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning