wiz-icon
MyQuestionIcon
MyQuestionIcon
1
You visited us 1 times! Enjoying our articles? Unlock Full Access!
Question

Consider the code fragment written in C below:
void f(int n)
{
if (n\(\le\) 1)
{
printf("%d", n);
}
else
{
f(n/2);
printf ("%d", n%2);
}
}

What does f(173) print?

Open in App
Solution

10101101 i.e Option (d)
Since recursive function calls will be
\(1^{st}:\) function call 173/2 = 86(int part only)
\(2^{nd}:\) function call 86/2 = 43
\(3^{rd}:\) function call 43/2 = 21(int part only)
\(4^{th}:\) function call 21/2 = 10 (int part only),
\(5^{th}:\) function call 10/2 = 5,
\(6^{th}:\) function call 5/2 = 2 (integer part only),
\(7^{th}:\) function call 2/2 = 1,
\(7^{th}:\) function call condition if (n <=1) will become true so n will be printed (i.e 1 will be printed) now while returning every function will execute its remaining part code i.e. print ('%d", n%2);
So
\(6^{th}:\) function will print 2 mod 2 = 0,
\(5^{th}:\) function will print 5 mod 2 = 1,
\(4^{th}:\) function will print 10 mod 2 = 0,
\(3^{th}:\) function will print 21 mod 2 = 1,
\(2^{nd}:\) function will print 43 mod 2 =1,
\(1^{st}:\) function will print 86 mod 2 = 0,
function call f(173) will print
173 mod 2 = 1

flag
Suggest Corrections
thumbs-up
0
Join BYJU'S Learning Program
similar_icon
Related Videos
thumbnail
lock
Introduction to Outsourcing
BUSINESS STUDIES
Watch in App
Join BYJU'S Learning Program
CrossIcon