(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
6. What value does function sum return when called with a value of 5?
+
What value does function sum return when called with a value of 5?
<syntaxhighlight lang="c">
+
<syntaxhighlight lang="c" name="recursion_1">
 
int sum (int n) {
 
int sum (int n) {
 
   if (n < 1) return 1;
 
   if (n < 1) return 1;
Line 8: Line 8:
  
  
===Solution===
+
==={{Template:Author|Arjun Suresh|{{arjunweb}} }}===
We have to find the value of sum(1), sum(2), sum(3), sum(4) and sum(5)
+
We have to find the value of sum(1), sum(2), sum(3), sum(4) and sum(5)
 +
sum(1) = 1 + sum(0) + sum(-1) = 1 + 1 + 1 = 3
 +
sum(2) = 2 + sum(1) + sum(0) = 2 + 3 + 1 = 6
 +
sum(3) = 3 + sum(2) + sum(1) = 3 + 6 + 3 = 12
 +
sum(4) = 4 + sum(3) + sum(2) = 4 + 12 + 6 = 22
 +
sum(5) = 5 + sum(4) + sum(3) = 5 + 22 + 12 = 39
 +
So, '''39''' is the answer
  
sum(1) = 1 + sum(0) + sum(-1) = 1 + 1 + 1 = 3
+
{{Template:FBD}}
 
 
sum(2) = 2 + sum(1) + sum(0) = 2 + 3 + 1 = 6
 
 
 
sum(3) = 3 + sum(2) + sum(1) = 3 + 6 + 3 = 12
 
 
 
sum(4) = 4 + sum(3) + sum(2) = 4 + 12 + 6 = 22
 
 
 
sum(5) = 5 + sum(4) + sum(3) = 5 + 22 + 12 = 39
 
 
 
So, '''39''' is the answer
 
 
 
<div class="fb-like"  data-layout="standard" data-action="like" data-show-faces="false" data-share="true"></div>
 
 
 
 
 
<div class="fb-share-button"  data-type="button_count"></div>
 
 
 
 
 
 
 
<disqus/>
 
  
 
[[Category:Coding Questions]]
 
[[Category:Coding Questions]]

Latest revision as of 14:00, 14 April 2014

What value does function sum return when called with a value of 5? <syntaxhighlight lang="c" name="recursion_1"> int sum (int n) {

  if (n < 1) return 1;
  else return n + sum(n - 1) + sum(n - 2);

} </syntaxhighlight>


Solution by Arjun Suresh

We have to find the value of sum(1), sum(2), sum(3), sum(4) and sum(5)
sum(1) = 1 + sum(0) + sum(-1) = 1 + 1 + 1 = 3
sum(2) = 2 + sum(1) + sum(0) = 2 + 3 + 1 = 6
sum(3) = 3 + sum(2) + sum(1) = 3 + 6 + 3 = 12
sum(4) = 4 + sum(3) + sum(2) = 4 + 12 + 6 = 22
sum(5) = 5 + sum(4) + sum(3) = 5 + 22 + 12 = 39
So, 39 is the answer




blog comments powered by Disqus

6. What value does function sum return when called with a value of 5? <syntaxhighlight lang="c"> int sum (int n) {

  if (n < 1) return 1;
  else return n + sum(n - 1) + sum(n - 2);

} </syntaxhighlight>


Solution[edit]

We have to find the value of sum(1), sum(2), sum(3), sum(4) and sum(5)

sum(1) = 1 + sum(0) + sum(-1) = 1 + 1 + 1 = 3

sum(2) = 2 + sum(1) + sum(0) = 2 + 3 + 1 = 6

sum(3) = 3 + sum(2) + sum(1) = 3 + 6 + 3 = 12

sum(4) = 4 + sum(3) + sum(2) = 4 + 12 + 6 = 22

sum(5) = 5 + sum(4) + sum(3) = 5 + 22 + 12 = 39

So, 39 is the answer



blog comments powered by Disqus