Question
What is the output of the following recursive function
call func(3) ? int func ( int n) {Â Â Â Â Â Â Â Â Â Â if (n == 0 ) return 1 ; Â Â Â return n * func(n - 1 ); }Solution
This function computes the factorial of n . Factorial is defined as n!=n×(n−1)× ⋯ ×1 . For n=3 :
- func(3) calls 3 * func(2) .
- func(2) calls 2 * func(1) .
- func(1) calls 1 * func(0) .
- func(0) returns 1 (base case).
- Option A (1): Only the base case returns 1 . This does not account for recursive multiplication.
- Option B (3): This is the input but not the factorial result.
- Option D (9): This result might confuse with 3² , which is unrelated to factorial.
- Option E (27): This is 3^3 .
A person travels from city A to city B at 60 km/h and returns at 40 km/h. If the total time taken for the round trip is 6 hours, find the distance betwe...
Let a plane pass through the point (1, –1, 2) and is perpendicular to the vector 2i + j − 2k. Then its Cartesian equation is:
A container has 80 liters of a mixture of milk and water, with 60% milk. If 10 liters of water are added, what will be the new percentage of milk in th...
A company’s quarterly sales (in lakh units) are:
Q1: 18, Q2: 24, Q3: 21, Q4: 27
Find the percentage increase from...
Find the area of triangle formed by the points A(1, 2), B(4, 6), and C(5, 2).
A cylinder of radius 12 cm containing water reduces its water level by 1 cm when a fully immersed hemi spherical ball is removed from this cylindrical c...
The average score of 24 students in a class is 78. If the top 4 scores are removed, the new average score of the remaining students drops by 2 points, w...
A shopkeeper sells an article at a profit of 12.5% on the selling price. Find the actual profit percentage.