You are on page 1of 4

1 Recursion :

2 ===========
3
4 int f(int n)
5 {
6 if(n==0||n==1)
7 return 1;
8 else
9 return n*f(n-1);
10 }
11 1.What is the Return value of the f(5) ?
12 2.How many function calls for f(n) ?
13 3.Maximum depth of function calls?
14 4.If T(n)= # of function calls for f(n) then Recurrence Relation of Equation ?
15 5.If T(n)= Return Value of f(n) then Recurrence Relation of Equation ?
16
17 ========================
18
19 int f(int n)
20 {
21 if(n==0||n==1)
22 return 1;
23 else
24 return f(n-1)*f(n-1);
25 }
26
27 ============================
28 int f(int n)
29 {
30 if(n==0)
31 return 0;
32 if(n==1)
33 return 1;
34 else
35 return f(n-1)*f(n-1);
36 }
37
38
39
40
41
42 ========================
43
44 int f(int n)
45 {
46 if(n==0||n==1)
47 return 1;
48 else
49 return 2*f(n-1)+3*f(n-2);
50 }
51
52 =========================
53
54 int f(int n)
55 {
56 int x=1,k;
57 if(n==1)
58 return x;
59 for(k=1;k<n;k++)
60 x=x+f(k)*f(n-k);
61 return x;
62 }
63
64 Consider the following C function.
65 int fun(int n)
66 { int x = 1, k;
67 if (n = = 1) return x;
68 for (k =1; k<n; ++k)
69 x = x + fun(k) * fun(n–k);
70 return x;
71 }
72 The return value of fun(5) is _____?
73
74
75
76
77 =============================
78
79 int x,result;
80 int f(int x)
81 {
82 x=x+1;
83 return x;
84 }
85
86 void main()
87 {
88 x=5;
89 result=f(x)*f(x);
90 printf("%d",result);
91 }
92
93 ===================================
94
95 What is the output printed by the following program ?
96 #include <stdio.h>
97 int f(int n, int k)
98 {
99 if(n == 0) return 0;
100 else if (n % 2) return f(n/2, 2 * k) + k;
101 else return f(n/2, 2 * k) – k;
102 }
103 int main ( )
104 {
105 printf (“%d”, f(20, 1));
106 }
107 (a) 5 (b) 8 (c) 9 (d) 20
108
109 ===========================================
110
111 What value would the following function return for the input x = 95?
112 Function fun (x: integer): integer;
113 begin
114 if x > 100 then return (x – 10);
115 else
116 return (fun(fun(x+11)));
117 end;
118 (a) 89 (b) 90
119 (c) 91 (d) 92
120
121 =======================================
122
123 int do(int n)
124 {
125 if(n<=2)
126 return 1;
127 else
128 return (do(floor(sqrt(n)))+n)
129 }
130
131 =======================================
132
133 void get(int n)
134 {
135 if(n<1)
136 return;
137 get(n-1);
138 get(n-3);
139 pf(%d",n);
140 }
141
142 =========================================
143
144 void foo(int n,int sum)
145 {
146 int k=0,j=0;
147 if(n==0) return;
148 k=n%10;
149 j=n/10;
150 sum=sum+k;
151 foo(j,sum);
152 pf("%d",k);
153 }
154
155 int main()
156 {
157 int a=2048,sum=0;
158 foo(a,sum);
159 pf("%d\n",sum);
160 }
161
162 ========================================
163
164 What is the result of the following program?
165 Program side-effect (input, output);
166 var x, result: integer;
167 function f (var x: integer):integer;
168 begin
169 x:= x+1;
170 f: = x;
171 end;
172 begin
173 x : = 5;
174 result: = f(x)*f(x);
175 writeln(result);
176 end
177 (a) 5 (b) 25
178 (c) 36 (d) 42
179
180 ========================================
181
182 What value would the following function return for the input x = 95?
183 Function fun (x: integer): integer;
184 begin
185 if x > 100 then return (x – 10);
186 else
187 return (fun(fun(x+11)));
188 end;
189 (a) 89 (b) 90
190 (c) 91 (d) 92
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216

You might also like