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...
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  ...
In Java In mathematics, factorial of a non-negative integer n, denoted by n! , is the...
In Java In mathematics, factorial of a non-negative integer n, denoted by n! , is the product of all positive integers less than or equal to n. For example, 5! = 5 * 4 * 3 * 2* 1 = 120 3! = 3 * 2 * 1 = 6 2! = 2 * 1 = 2 The value of 0! is 1. Write a program that asks user to enter an integer > 0; if a valid value is...
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 ? ??,...
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...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT