Recent questions tagged recursion

5 5 votes
1 1 answer
402
402 views
Consider the following Python code:def virfib_sq(n): print(n) if n <= 1: return n return (virfib_sq(n - 1) + virfib_sq(n - 2)) 2 r4 = virfib_sq(4)What would be the outp...
3 3 votes
1 1 answer
227
227 views
Consider the following Python code:def virfib_sq(n): print(n) if n <= 1: return n return (virfib_sq(n - 1) + virfib_sq(n - 2)) 2 r3 = virfib_sq(3)What would Python disp...
2 2 votes
1 1 answer
196
196 views
Consider the following Python code:def virfib_sq(n): print(n) if n <= 1: return n return (virfib_sq(n - 1) + virfib_sq(n - 2)) 2 r2 = virfib_sq(2)What would be the outp...
2 2 votes
1 1 answer
192
192 views
Consider the following Python code:def virfib_sq(n): print(n) if n <= 1: return n return (virfib_sq(n - 1) + virfib_sq(n - 2)) 2 r1 = virfib_sq(1)What would be the outp...
1 1 vote
1 1 answer
215
215 views
Consider the following Python code:def virfib_sq(n): print(n) if n <= 1: return n return (virfib_sq(n - 1) + virfib_sq(n - 2)) 2 r0 = virfib_sq(0)What would be the outp...
1 1 vote
1 1 answer
218
218 views
Consider the following Python code:def total_first(t): if not t: return 0 return t[0][0] + total_first(t[1:]) data = [[2, 7], [5, 1], [3, 8], [4, 9]] print(total_first(da...
1 1 vote
1 1 answer
170
170 views
Consider the following Python code:def show(lst, i=0): if i == len(lst): return show(lst, i + 1) if lst[i] % 2 != 0: print(lst[i], end="") show([1, 2, 3, 4, 5])What is th...
1 1 vote
1 1 answer
158
158 views
Consider the following Python code:def count_greater(lst, v): if not lst: return 0 count = 1 if lst[0] v else 0 return count + count_greater(lst[1:], v) print(count_grea...
1 1 vote
1 1 answer
157
157 views
Consider the following Python code:def mystery(n): if n < 10: return n a = n // 10 b = n % 10 return mystery(a + b) print(mystery(678))What is the output of the code abov...
1 1 vote
1 1 answer
159
159 views
Consider the following Python code:def solve(n): if n <= 0: return 0 return n + solve(n - 2) print(solve(6), solve(5))What is the output of the code above?9 1212 912 12Re...
8 8 votes
3 3 answers
389
389 views
What is the output of the following code?#include <stdio.h void update(int n, int *p) { if (n <= 0) return; *p = *p + n; update(n - 2, p); *p = *p + n; } int main() { int...
6 6 votes
2 2 answers
256
256 views
What is the output of the following code?#include <stdio.h int fun(int n) { if (n <= 0) return 1; if (n == 1) return 2; return fun(n - 1) + 2 * fun(n - 2); } int main() {...
6 6 votes
2 2 answers
329
329 views
What is the output of the following code?#include <stdio.h int fun(int n) { int x = n; if (n <= 0) return 0; x = x + 2; return x + fun(n - 2); } int main() { printf("%d",...
6 6 votes
2 2 answers
349
349 views
How many times is $\texttt{fun()}$ called when the following code is executed? (Count the first call also).#include <stdio.h int fun(int n) { if (n <= 1) return 1; return...
6 6 votes
3 3 answers
336
336 views
What is the output of the following code?#include <stdio.h int fun(int n) { if (n <= 1) return n + 1; if (n % 2 == 0) return fun(n - 1) + fun(n - 2); return fun(n - 2) + ...
5 5 votes
4 4 answers
340
340 views
What is the output of the following code?#include <stdio.h void g(int n); void f(int n) { if (n <= 0) return; printf("F%d ", n); g(n - 1); printf("f%d ", n); } void g(int...
6 6 votes
3 3 answers
320
320 views
What is the output of the following code?#include <stdio.h void fun(int n) { static int x = 0; if (n == 0) return; x++; printf("%d:%d ", n, x); fun(n - 1); printf("%d:%d ...
5 5 votes
2 2 answers
274
274 views
What is the output of the following code?#include <stdio.h int fun(int n) { static int sum = 0; int temp; if (n <= 0) return sum; sum = sum + n; temp = fun(n - 2); return...
6 6 votes
3 3 answers
253
253 views
What is the output of the following code?#include <stdio.h int fun(int n) { static int step = 1; if (n <= 1) return n; step++; return n + fun(n - step); } int main() { pr...
5 5 votes
3 3 answers
307
307 views
What is the output of the following code?#include <stdio.h int fun(int n) { if (n == 0) return 1; return n + fun(n - 1); printf("%d ", n); } int main() { printf("%d", fun...
7 7 votes
4 4 answers
386
386 views
What is the output of the following code?#include <stdio.h void fun(int n) { if (n <= 0) return; printf("%d ", n); fun(n - 1); printf("%d ", n); fun(n - 2); } int main() ...
7 7 votes
3 3 answers
276
276 views
What is the output of the following code?#include <stdio.h void g(int n); void f(int n) { if (n <= 0) return; printf("%d ", n); g(n - 1); } void g(int n) { if (n <= 0) re...
7 7 votes
3 3 answers
286
286 views
What happens when the following code is executed?#include <stdio.h void fun(int n) { if (n == 0) return; printf("%d ", n); fun(n ); } int main() { fun(3); return 0; }$\te...
6 6 votes
3 3 answers
289
289 views
What is the output of the following code?#include <stdio.h int fun(int n) { static int x = 0; int y; if (n == 0) return 0; x++; y = fun(n - 1); return y + x; } int main()...
6 6 votes
4 4 answers
360
360 views
What is the output of the following code?#include <stdio.h void fun(int n) { if (n == 0) return; printf("%d ", n); fun(n - 1); printf("%d ", n); } int main() { fun(3); re...
6 6 votes
2 2 answers
255
255 views
What is the output of the following code?#include <stdio.h int fun(int n) { static int count = 0; if (n == 0) return count; count = count + n; return fun(n - 1); } int ma...
5 5 votes
2 2 answers
211
211 views
What is the output of the following code?#include <stdio.h int fact(int n) { if (n == 0) return 1; return n * fact(n - 1); } int main() { printf("%d", fact(4)); return 0;...
5 5 votes
2 2 answers
222
222 views
What is the output of the following code?#include <stdio.h int sum(int n) { if (n == 1) return 1; return n + sum(n - 1); } int main() { printf("%d", sum(5)); return 0; }
3 3 votes
2 2 answers
197
197 views
What is the output of the following code?#include <stdio.h void fun(int n) { if (n == 0) return; fun(n - 1); printf("%d ", n); } int main() { fun(4); return 0; }$\texttt{...
4 4 votes
2 2 answers
226
226 views
What is the output of the following code?#include <stdio.h void fun(int n) { if (n == 0) return; printf("%d ", n); fun(n - 1); } int main() { fun(4); return 0; }$\texttt{...