Computer Science, asked by katyakatthi24, 8 months ago

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 sandeep48kr
49

Answer:

it will call for 16 times

Answered by jaya8765
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

  1. (30 + sum(28))
  2. (30 + sum(26))
  3. (30 + sum(24))
  4. (30 + sum(22))
  5. (30 + sum(20))
  6. (30 + sum(18))
  7. (30 + sum(16))
  8. (30 + sum(14))
  9. (30 + sum(12))
  10. (30 + sum(10))
  11. (30 + sum(8))
  12. (30 + sum(6))
  13. (30 + sum(4))
  14. (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