Question

In: Computer Science

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 to 0 and returns -1 if the input is a negative integer.

Test the function with the following values: 5! = 120, 6! = 720, 7! = 5040, 8! = 40320, 9! = 362880.

Solutions

Expert Solution

VBA code:

Function MyFactorial(Value As Long) As Long
    If Value = 0 Or Value = 1 Then
        MyFactorial = 1
    ElseIf Value < 0 Then
        MyFactorial = -1
    Else
        MyFactorial = MyFactorial(Value - 1) * Value
    End If
End Function

Screenshot of code:

Output:

Steps to run the code:

1) Write the code in the VBA editor of Excel

2) Go to sheet

3) Make a column for 'Number' and 'Factorial'

4) Write the numbers you need the factorial for in the 'Number' column

5) Under the factorial column write the formula "=MyFactorial("Cell number")" and press enter.


Related Solutions

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...
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 ? ??,...
For two sets ? and ?, their Jaccard similarity, denoted ?(?,?), is defined as ?(?,?) =...
For two sets ? and ?, their Jaccard similarity, denoted ?(?,?), is defined as ?(?,?) = |? ∩ ?| |? ∪ ?| where |?| is the size of set ?; ? ∩ ? is the intersection of ? and ?, that is the elements in both of ? and ?; and ? ∪ ? is the union of ? and ?, that is the elements in at least one of ? and ?. For example, if ? = {1,2,3,4} and...
A triangular number is the sum of the n natural numbers from 1 to n. For...
A triangular number is the sum of the n natural numbers from 1 to n. For example: The triangular number for 3 is 1 + 2 + 3 = 6 The triangular number for 7 is 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28 Write a program segment (using a loop), that calculates and then prints the integer n and its triangular number.
Write a recursive function to calculate and return factorial of a given number 'n'. in C...
Write a recursive function to calculate and return factorial of a given number 'n'. in C progrmaining
Directions: Given a factorial n!. Find the sum of its digits, and the number of trailing...
Directions: Given a factorial n!. Find the sum of its digits, and the number of trailing zeros (ie: the number of zeros at the end of the factorial). Input: integer n , a non-negative integer where n ≤ 100 Output: integer x y , the concatenation of x and y, where x is the sum of the digits in n! and y is the number of the zeros in n!) Note, 0 ≤ x , y . Example: Consider the...
(1) Write a simple Lisp function factorial1 to computee factorial number of n recursivaly (where n...
(1) Write a simple Lisp function factorial1 to computee factorial number of n recursivaly (where n is an int >= 0). (2) Write a Lisp function factorial2 to computee factorial number of n (where n is an int >=0) recursivaly with a global variable (e.g., a list, an array, or a hash tble) to save the result to be used later (memorization) to compute next factorial number. Run the program with test case. For your test run, the following cases:...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT