The function given below takes an even integer "n" as the input and calculates the
sum of first "n" even natural numbers. The function is called by the statement
"sum(30)". How many times will the function "sum" be called to compute the sum?
function sum(n)
if (n equals 2)
return 2
else
return (n+sum(n-2))
end
Answers
Answered by
49
Answer:
it will call for 16 times
Answered by
3
Answer:
The function will be called 16 times
Explanation:
The given program is :
function sum( n )
{
if(n equals 2)
return 2
else
return (n + sum(n-2))
end
}
Debugging:
let n = 30
then the cursor moves to if condition
here n ≠ 2
so the cursor again moves to else block
then return ( 30 + sum( 30 - 2) )
end
like this the value will return value
- (30 + sum(28))
- (30 + sum(26))
- (30 + sum(24))
- (30 + sum(22))
- (30 + sum(20))
- (30 + sum(18))
- (30 + sum(16))
- (30 + sum(14))
- (30 + sum(12))
- (30 + sum(10))
- (30 + sum(8))
- (30 + sum(6))
- (30 + sum(4))
- (30 + sum(2))
and the if condition will be satisfied and it will return the value 2 again
hence the function will be called 16 times .
To know more about c programs and the basic programs in c visit the links given below:
https://brainly.in/question/36036687
https://brainly.in/question/6474841
Similar questions
Computer Science,
4 months ago
Physics,
4 months ago
English,
4 months ago
English,
8 months ago
Geography,
8 months ago
English,
11 months ago
Math,
11 months ago
Accountancy,
11 months ago