0% found this document useful (0 votes)
42 views8 pages

DAY-4 Topics: Recursion, Storage Classes

1. The document discusses recursion and storage classes. It provides examples of recursive functions to calculate factorials and print patterns, as well as examples using static variables. 2. Multiple functions are defined and called recursively to perform calculations and print outputs. Static variables are used to retain values between recursive function calls. 3. Examples include recursive functions to calculate factorials, print patterns, calculate Fibonacci series terms, and demonstrate how static variables persist scope between recursive calls.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views8 pages

DAY-4 Topics: Recursion, Storage Classes

1. The document discusses recursion and storage classes. It provides examples of recursive functions to calculate factorials and print patterns, as well as examples using static variables. 2. Multiple functions are defined and called recursively to perform calculations and print outputs. Static variables are used to retain values between recursive function calls. 3. Examples include recursive functions to calculate factorials, print patterns, calculate Fibonacci series terms, and demonstrate how static variables persist scope between recursive calls.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

DAY-4

Topics: Recursion, Storage Classes

1. int fact(int n, int f)


{
if(n==1)
return f;
else
return fact(n-1, n*f);
}
main()
{
int temp;
temp=fact(5,1);
printf("The value of factorial 5 is %d",temp);
}
Ans: 120

2. What will be the output of the following procedure if n=3

void count(Int n)
{

static d=1;
print(n);
print(d);
d++;
if(n>1)
count(n-1);
print(d);
}
Ans: 3 1 2 2 1 3 4 4 4

3. What is the output of the following program?

intfuncf (int x);


intfuncg (int y);

main()
{
int x = 5, y = 10, count;
for (count = 1; count <= 2; ++count)
{
y += funcf(x) + funcg(x);
printf ("%d ", y);
}
}

funcf(int x)
{
int y;
y = funcg(x);
return (y);
}

funcg(int x)
{
staticint y = 10;
y += 1;
return (y+x);
}
Ans: 43, 80

4. How many asterixare printed if fun(5) is called?

fun(int n)
{
int i = 0
if (n > 1)
fun(n-1)
for (i = 0; i < n; i++)
Print ("* ")
}

Ans: 15

5. Find output of the following.


int fun ( int n, int *fp )
{
int t, f;

if ( n<= 1 )
{
*fp = 1;
return 1;
}
t = fun ( n-1, fp );
f = t + *fp;
*fp = t;
return f;
}

int main()
{
int x = 15;
printf("%d\n",fun(5, &x));

return 0;
}
Ans: 8
6. Find output of the following C code for fun(4, 3).
int fun(Int a, Int b)
{
if (b == 0)
return 0;
if (b % 2 == 0)
return fun(a+a, b/2) ;
return fun(a+a, b/2) + a;
}
Ans: 12

7. Find output of the following code for fun1(8).

int fun1(int n)
{
if(n == 1)
return 0 ;
else
return 1 + fun1(n/2) ;
}
Ans: 3

8. What does the above program print foo (2048, 0)?

void foo(int n, int sum)


{ int k = 0, j = 0;
if (n == 0)
return;
k = n MOD 10;
j = n / 10;
sum = sum + k;
foo (j, sum);
printf (“%d “,k);
}

Ans: 2,0,4,8

9. What will be the output of the following code if we call fun(2) ?

int fun (int n)


{
if (n == 4)
return n;
else
return 2 * fun (n + 1);
Ans:16

10. What will be the output of the following code?

void reverse(int n)
{
if (n > 5)
exit(0);
printf("%d\n", n);
return reverse(n++);
}
int main()
{
reverse(1);
}
Ans: infinite recursion

11. What will be the output of the following code for fun(99)?

int fun(int n)
{
if (n > 100)
return n – 10;
return fun(fun(n+11));
}
Ans: 91

12. main()
{
int i=5;
printf("%d\n",i);
i--;
if(i)
main();
}
Ans: 1,1,1,1,1,-----infinite times

13. What will be the output of the following pseudocode for fun("pqr") ?

fun(char *a)
if (*a EQUALS NULL)
return
end if
fun(a+1)
fun(a+1)
print *a

Ans: rrqrrqp

14. main()
{
static int i=5;
printf("%d\n",i);
i--;
if(i)
main();
}
.

15. int var=100;


fun()
{
int var=50;
printf("The value of var in fun is %d\n",var);
}
main()
{
printf("The value of var in main is %d\n",var);
var=var+200;
fun();
}

16. fun()
{
int var=100;
printf("The value of var in fun is %d\n",var);
}
main()
{
printf("The value of var in main is %d\n",var);
var=var+200;
fun();
}

17. fun(int var)


{
printf("The value of var in fun is %d\n",var);
}
main()
{
int var=100;
printf("The value of var in main is %d\n",var);
fun(var/2);
}

18. int var=200;


int var=250;
main()
{
printf("Multiple definitions");
}

19. auto int var=200;


main()
{
printf("The value of var is %d",var);
}

20. main()
{
int exp=1,j=10;
switch(exp)
{
case 1:
{
int j=2;
printf("The value of j in case 1 is %d\n",j);
}
case 2:
{
printf("The value of j in case 2 is %d\n",j);
}}
}

21. main()
{
extern int var;
printf("The value of var is %d",var);
}

22. main()
{
extern int var;
printf("The value of var is %d",var);
}
int var=200;

23. main()
{
extern int var=200;
printf("The value of var is %d",var);
}

24. function(static int para)


{
printf("The value of parameter is %d",para);
}

main()
{
int var=200;
printf("The value of var is %d",var);
function(var);
}

25. int a=200;


main()
{
int b=300;
printf("The values in outer block of main are %d%d\n",a,b);//<-line 1
{
int b=400;
printf("The values in inner block of main are %d%d\n",a,b);//<-line 2
}
printf("The values back in outer block of main are %d%d\n",a,b);//<-line 3
}
26.
one.c two.c
extern_function() int var=200;
main() extern_function()
{ {
extern int var; printf("Function in other translation unit");
printf("The value of external var is %d\n",var); }
extern_function(); }
7. 7.
one.c two.c
static_function() static int var=200;
main() static static_function()
{ {
extern int var; printf("Function in other translation unit");
printf("The value of external var is %d\n",var); }
static_function();
}

27.
one.c two.c
int var=200; int var=200;
function() function()
{ {
printf("Function in same translation unit"); printf("Function in other translation unit");
} }
main()
{
printf("The value of external var is %d\n",var);
function();
}

28. fib_term()
{
int a=0,b=1;
int c;
c=a+b;a=b;b=c;
return c;
}
main()
{
int count=0,i;
printf("First five terms of Fibonacci series are \n");
for(i=0;i<5;i++)
printf("%d",fib_term());
}

29. fib_term()
{
static int a=0,b=1;
int c;
c=a+b;a=b;b=c;
return c;
}
main()
{
int count=0,i;
printf("First five terms of Fibonacci series are \n");
for(i=0;i<5;i++)
printf("%d",fib_term());
}

You might also like