0% found this document useful (0 votes)
6 views5 pages

C-Programming Fun & Recursion DPP-02

The document contains a series of programming problems focused on recursion in C, with various functions and their expected outputs. Each problem is followed by a solution that explains how the output is derived through recursive calls. An answer key is provided at the end for quick reference to the correct answers for each problem.

Uploaded by

Anbu Alwin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

C-Programming Fun & Recursion DPP-02

The document contains a series of programming problems focused on recursion in C, with various functions and their expected outputs. Each problem is followed by a solution that explains how the output is derived through recursive calls. An answer key is provided at the end for quick reference to the correct answers for each problem.

Uploaded by

Anbu Alwin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

C-PROGRAMMING (FUN & RECURSION)

DPP - 02
1. Consider the following function/method: 5. Consider the following recursive function.
int test(int a, int b) int func(int a, int n)
{ { //you may assume n >=0}
if(a<b) if (n==0)
return 0; return 0;
else return (1+ test(a–b,b)); else
} return a + func(a, n-1);
What is returned by the call test(15,4)? }
A. 1 B. 2 The return value of the call func(4,3) is.
C. 3 D. 4
6. Consider the following function:
2. Consider the following recursive function: int func(int a)
int Fun (int n) {
{ static int b=1;
if (n == 4) b=b+a;
return 2; if((b%a)%2!=0) return a+func(b+a)
else return b-a;
return 2 * Fun (n + 1); }
} The value returned by func(5) is
What is the value returned by function Fun(2)? func(5):
A. 2 B. 4
C. 8 D. 16 7. Consider the following function:
void func(int n)
3. Consider the following function: {
intF(int n ) { if(n>0){
if(n > 100) func(n-1);
return(n-10); printf("%d\t", n);
else }
return F( F(n+11) ) ; printf("%d\t", n-1);
} }
What would be returned for the call F (99)? The output printed by func(2) is-
(a) –1 1 0 2 1 (b) –1 1 0 2 1 2
4. Consider the following code (c) –1 1 0 2 -2 1 (d) –1 1 0 2 1
Consider the following function given below:
int function (int n) 8. Consider the following function:
{ int func(int n)
if(n–1) {
return 2*function (n–1)+n;dc if(n>0){
else return 3*func(n/4)+1;
return 0; }
} return n;
What is the value returned by function (5)? }
A. 33 B. 41 The value returned by func(24) is
C. 57 D. 65
C-PROGRAMMING (FUN & RECURSION)

DPP - 02
9. Consider the following function:
int func(int n, int i)
{
if(n==0) return 0;
else if(n%2){
return func(n/2, 2*i)+i;
}else return func(n/2, 2*i)-i;
}
The value returned by func(14, 1) is-
(a) 1 (b) 13
(c) 15 (d) 0

10. Consider the following function:


int func(int n)
{
static int i=0;
if(n/2){
i--;
return func(n/2)+i;
}else return i;
}
The value returned by func(7) is-
(a) –6 (b) –12
(c) –18 (d) –21
C-PROGRAMMING (FUN & RECURSION)

DPP - 02

ANSWER KEY
1. C
2. A
3. 91
4. D
5. 12
6. 11
7. D
8. 13
9. B
10. A
C-PROGRAMMING (FUN & RECURSION)

DPP - 02

HINTS & SOLUTIOS Line 1: return a+func(b+a);// return 5+func(6+5);


func(11) is called. Returns 5+6 i.e 11.
1. Solution: Line 2: return b-a;
We get the answer by tracing through the series of func(11):
recursive calls: static int b=1; //static b is initialized to 1.
test(15,4): 15 > 4, so make a recursive call to test(15-4,4) b=b+a; //b=6+11=17
test(11,4): 11 >4, so make a recursive call to test(7,4) if((b%a)%2!=0) //(17%11)%2!=0 is FALSE
test(7,4): 7 > 4, so make a recursive call to test(3,4) Line 1: return a+func(b+a);
test(3,4): 3 < 4, so we’ve hit the base case and return0 Line 2: return b-a; // return (17-11) i.e 6 to Line 1
test(7,4) : return 1+0 = 1 of func(5);
test(11,4): return 1+1 = 2
test(15,4): return 1+ 2 = 3 7. Solution:
func(2):
2>0 True
2. Solution:
func(1) is called.
Fun(4) is 2. Fun(3) is 2*Fun(4) i.e. 4.
printf("%d\t", n); // 2 is printed.
Fun(2) is 2*Fun(3) i.e. 8.
printf("%d\t", n-1); //1 is printed.
func(1):
3. Solution:
1>0 True
F(99) will return F(F(99+11)), which means final
func(0) is called.
output will be F(F(110)).
printf("%d\t", n); // 1 is printed.
F (110) will return 110-10 = 100
printf("%d\t", n-1); //0 is printed.
F(100) will return F(F(111))
func(0):
F(111) will return 101.
0>0 is FALSE
F(101) will return 91.
printf("%d\t", n-1); //-1 is printed.
So, F(99) will return F(F(110)) which is F(100) which is
Output: -1 1 0 2 1
91.

8. Solution:
4. Solution:
func(24):
According to program,
if(n>0){ //24>0 is TRUE
(n) = n + 2F(n–1) for n>=2
return 3*func(n/4)+1; //func(6) is called.
= 0 for n=1
Returns 3*4+1; Returns 13.
F(5) = 5+2(4+2(3+2(2+2(0)))) = 41
}
func(6):
5. Solution:
if(n>0){ //6>0 is TRUE
This function returns product of a,n. So, func(4,3) will
return 3*func(n/4)+1; //func(1) is called.
return 4*3 = 12.
Returns 3*1+1; returns 4;
In every function call, value of n is decreasing by one
}
And every function call is returning a + func(a, n-1)
func(1):
where a remains constant/same in all function calls.
if(n>0){ //1>0 is TRUE
return 3*func(n/4)+1; //func(0) is called.
6. Solution:
Returns 1;
static int b=1; //static b is initialized to 1.
}
b=b+a; //b=1+5=6
func(0):
if((b%a)%2!=0) //(6%5)%2!=0 is TRUE
C-PROGRAMMING (FUN & RECURSION)

DPP - 02
return 0;

9. Solution:
func(14, 1):
14%2 is 0, so else part is executed.
return func(7, 2)-1;// //Returns 14-1 i.e 13.
func(7,2):
7%2 is 1, so else if part is executed.
return func(3, 4)+2; //Returns 12+2 i.e 14 to
func(14, 1)
func(3,4):
3%2 is 1, so else if part is executed.
return func(1, 8)+4; //Returns 8+4 i.e 12 to func(7, 2)
func(1,8):
1%2 is 1, so else if part is executed.
return func(0, 16)+8; //fun(0,16) returns 0
//Returns 8 to func(3, 4)

10. Solution:
func(7):
static int i=0;
if(n/2){//7/2= 3 is TRUE
i--;//static i is decremented to -1
return func(n/2)+i; //func(3) is called. func(7)
returns -4-2 i.e -6
}else return i;
func(3):
static int i=0;
if(n/2){//3/2= 1 is TRUE
i--;//static i is decremented to -2
return func(n/2)+i; //func(1) is called. func(1)
returns -2. func(3) returns -2-2 i.e -4
}else return i;

You might also like