Question

In: Mechanical Engineering

The factorial of an integer ? is defined using the following equation: ?! = ? ∗...

The factorial of an integer ? is defined using the following equation: ?! = ? ∗ (? − 1) ∗ (? − 2) ∗ … ∗ 2 ∗ 1 In combinatorics, permutations is the number of ways of selecting a subset of some elements such that the order matters. For example, there are 6 permutations of letters A, B, and C. A B C ,A C B, B A C, B C A, C A B, C B A, The permutation of selecting ? elements from a set of ? elements can be calculated using the following equation: ?(?, ?) = ?! (? − ?)! On the other hand, combinations is the number of ways of selecting a subset of some elements such that the order does not matter. The combination of selecting elements from a set of m elements can be calculated using the following equation: ?(?, ?) = ( ? ? ) = ?! ?! (? − ?)! Instructions Add a section / cell in your MATLAB m-file script for problem 3 that: • Prompts the user to enter integer ? between 2 and 12 (inclusive) o If the input is invalid, displays an error message and prompt the user to try again, as many times as necessary (use a loop to accomplish this instead of terminating the program) • Prompts the user to enter integer ? between 2 and ? (inclusive) o If the input is invalid, displays an error message and prompt the user to try again, as many times as necessary (use a loop to accomplish this instead of terminating the program) • Determines and displays the following: o ?! o ?(?, ?) o ?(?, ?) This program must use loops to calculate the results – DO NOT use any pre-defined MATLAB functions related to factorials, permutations, and/or combinations.

Solutions

Expert Solution

MATLAB script is given below-

clear
clc
%loop to get the value of n from user
while (1)
n=input('Enter the integer between 2 and 12 (inclusive): ');
if n>2 && n<=12
break
end
end

%loop to get the value of k from user
while (1)
fprintf('Enter the integer between 2 and %d (inclusive)',n)
k=input(': ');
if k>2 && k<=n
break
end
end
% to calculate the factorial of n!
fact_n=1;

for i=1:n
fact_n=i*fact_n;
end
% to calculate the factorial of k!
fact_k=1;

for i=1:k
fact_k=i*fact_k;
end
% to calculate the factorial of (n-k)!
fact_n_k=1;

for j=1:(n-k)
fact_n_k=fact_n_k*j;
end
% to caculate the values of P(n,k) and C(n,k)
P_n_k=fact_n*fact_n_k; C_n_k=fact_n*fact_n_k*fact_k;
% to print the values on command window
fprintf('The value of P(n,k)=%d, C(n,k)=%d and n!=%d\n\n\n',P_n_k,C_n_k,fact_n)

-----------------------------------------------------------------------------------------------------------------------------------

Output-

Enter the integer between 2 and 12 (inclusive): 7
Enter the integer between 2 and 7 (inclusive): 4
The value of P(n,k)=30240, C(n,k)=725760 and n!=5040


Related Solutions

The factorial of a nonnegative integer n is written n! (Pronounced “n factorial”) and is defined...
The factorial of a nonnegative integer n is written n! (Pronounced “n factorial”) and is defined as follows: n! = n.(n-1). (n-2).……...1 (for values of n greater than 1) n!=1 (for n = 0 or 1) For instance, 5! = 5.4.3.2.1 which is 120. Write a Python program that reads a nonnegative integer n and calculates and prints its factorial. Your program should display a suitable error message if n entered as a negative integer.    Figure   6.   Exercise   6  ...
The factorial of a non-negative integer is defined as follows: n! = 1 × 2 ×...
The factorial of a non-negative integer is defined as follows: n! = 1 × 2 × ... × (n − 1) × n A. Write a function that takes an input value n and returns its factorial result n!. Ensure that your function returns an error statement if the input n is either a negative or a non-integer value. You cannot use the prod() or factorial() functions. The Euler number e is calculated as the sum of the infinite series...
In C++ Let n be a non-negative integer. The factorial of n, written n!, is defined...
In C++ Let n be a non-negative integer. The factorial of n, written n!, is defined by 0 ! 5 1, n! = 1·2·3· · ·n if n $ 1. For example, 4! = 1·2·3·4 = 24. Write a program that prompts the user to enter a nonnegative integer and outputs the factorial of the number. Allow the user to repeat the program. Example: If the user enters a 3 then your program should perform answer = 3 * 2...
Create a program that will calculate the factorial of any user entered integer. For any positive integer, the factorial is given as
Create a program that will calculate the factorial of any user entered integer. For any positive integer, the factorial is given as: n! = n·(n-1)·(n-2)·.……·3·2·1. The factorial of 0 is 1. If a number is negative, factorial does not exist and this program should display error message. Sample output dialogues should look like these:  Enter an integer: -5 ERROR!!! Tactorial of a negative number doesn't exist  Enter an integer: 10  Factorial = 3628800
Assignment Instructions: 1) The Factorial The factorial of a non-negative integer ??, denoted by ??!, is...
Assignment Instructions: 1) The Factorial The factorial of a non-negative integer ??, denoted by ??!, is the product of all positive integers less than or equal to ??. The textbook has an example of a recursive MIPS implementation of factorial. Additionally, a simplified version of the MIPS assembly language recursive implementation of the factorial function is attached. Trace the factorial example carefully using QTSPIM 2) Recursive definition of multiplication The function ??????????(??, ??) for two positive integers 1 ? ??,...
Assignment Instructions: 1) The Factorial The factorial of a non-negative integer ??, denoted by ??!, is...
Assignment Instructions: 1) The Factorial The factorial of a non-negative integer ??, denoted by ??!, is the product of all positive integers less than or equal to ??. The textbook has an example of a recursive MIPS implementation of factorial. Additionally, a simplified version of the MIPS assembly language recursive implementation of the factorial function is attached. Trace the factorial example carefully using QTSPIM 2) Recursive definition of multiplication The function ??????????(??, ??) for two positive integers 1 ? ??,...
In mathematics, the notation n! represents the factorial of the nonnegative integer n. The factorial of...
In mathematics, the notation n! represents the factorial of the nonnegative integer n. The factorial of n is the product of non-negative numbers from 1 to n. Design a program that asks the user to enter a nonnegative integer and then displays the factorial of that number. Module main. Asks the user to enter a non-negative integer. A loop is used to require user input until a nonnegative number is entered. Once a nonnegative number is entered, the integer is...
a program that takes an integer and outputs the resulting factorial number.
assembly x86 language program a program that takes an integer and outputs the resulting factorial number. 
On a raspberry pi, write an assembler program that will calculate the factorial of an integer...
On a raspberry pi, write an assembler program that will calculate the factorial of an integer value inputted by the user. the program should detect if an overflow occurs. If no overflow occurred, then it should print out the value of the factorial. Otherwise print out a message indicating that an overflow occurred.
The factorial of a natural number n is denoted by n! and is defined as follows:...
The factorial of a natural number n is denoted by n! and is defined as follows: the factorial of 0 is 1, the factorial of 1 is 1, and the factorial of any other positive integer is the product of all the integers from 2 to that integer. Write a VBA function called MyFactorial with one input of data type Integer which computes and returns the value of the factorial of the input if it is greater than or equal...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT