179k views
2 votes
What is the result of executing the following code? You can assume the code compiles and runs. #include using namespace std; void factorial(int n) { cout << n << '*'; factorial(n-1); } int main() { factorial(4); return 0; }

1 Answer

7 votes

Answer:

The result of executing the code is 24.

Step-by-step explanation:

Factorial of a number:

The factorial of a number is the multiplication of a number by all it's previous numbers until one. For example:

0! = 1

1! = 1

2! = 2*1 = 2

3! = 3*2*1 = 6

4! = 4*3*2*1 = 24

In this question:

This is a C++ code, which is a recursive function to calculate a factorial of a number.

The input, given by factorial(4), is 4, so the result of executing the code is 24.

cout << n << '*'; factorial(n-1);

This means for each input, until n = 1, the output is the factorial of the number. This is the recursive function.

User SKulibin
by
4.4k points