23 23 votes Consider the code fragment written in C below : void f (int n) { if (n <= 1) { printf ("%d", n); } else { f (n/2); printf ("%d", n%2); } } Which of the following implementations will produce the same output for $f(173)$ as the above code? P1 P2 void f (int n) { if (n/2) { f(n/2); } printf ("%d", n%2); } void f (int n) { if (n <=1) { printf ("%d", n); } else { printf ("%d", n%2); f (n/2); } } Both $P1$ and $P2$ $P2$ only $P1$ only Neither $P1$ nor $P2$ Algorithms gateit-2008 algorithms recursion identify-function normal + – Ishrat Jahan 12.9k views answer comment Share Follow Print See all 6 Comments 6 6 Comments reply Show 3 previous comments ꧁༒☬ĿọŗԀ 🆂🅷🅸🆅🅰☬༒꧂ commented Jul 5, 2024 reply Follow flag void fun() { static int n=173; if(n==0) return; n=n/2; fun(n); printf("%d" , n%2); } actually i was thinking abt this case static is there it will always Print 0 0 0 replyShare Shaik Masthan commented Jul 5, 2024 reply Follow flag Static int n; --- this line demands a compile time constant. It can't take the value at the run time. 1 1 replyShare js__ commented Jan 25 reply Follow flag P2 is printing in reverse binary represntation 0 0 replyShare Please log in or register to add a comment.
Best answer 30 30 votes Answer: C The code fragment written in C and P1 prints the binary equivalent of the number n. P2 prints the binary equivalent of the number n in reverse. Rajarshi Sarkar answered Apr 15, 2015 • selected Jun 29, 2019 by Arjun Rajarshi Sarkar comment Share Follow See all 2 Comments 2 2 Comments reply Srijita Ghosh commented Apr 28, 2024 reply Follow flag Isn't p1 is also printing one extra 0 at starting and the representation becomes 010101101 2 2 replyShare Shaik Masthan commented Jul 5, 2024 reply Follow flag Let's take 5 as input. Then P1 printing that N= 5 ---- this will call f(2) and append 1 at the end N=2 --- this will call f(1) and append 0 at the end N=1 --- this will not enter into the if(), and directly return with 1. In sum up, after returning 1, append 0, then append 1. N=5, output = 101 1 1 replyShare Please log in or register to add a comment.
36 36 votes Here, $P1$ and $P2$ will print opposite in direction as shown in diagram. And given code fragment will print like $P1$ and not like $P2$ Hence, answer will be (C). srestha answered Jan 2, 2016 • edited Jul 1, 2019 by ajaysoni1924 srestha comment Share Follow See all 9 Comments 9 9 Comments reply Rajesh Pradhan commented Nov 13, 2016 reply Follow flag nice answer. 3 3 replyShare srestha commented Nov 13, 2016 reply Follow flag tnks :) 1 1 replyShare Pritam Dutta commented Oct 31, 2017 reply Follow flag P1 is printing a leading 0,so shouldn't it be option (D) ? 1 1 replyShare krish__ commented Nov 22, 2017 i edited by krish__ Nov 22, 2017 reply Follow flag There is no leading 0 printed. On reaching f(1) in P1, there's a check if(n/2){..}. The check fails and does not result in 0 being printed since f(0) is not invoked. 1 1 replyShare Ayush Upadhyaya commented Aug 22, 2018 reply Follow flag once we know what the code is doing take smaller inputs on which output is not a palindrome so that you are able to make a difference which function is doing what like binary of 6 is 110(This binary output is not a palindrome), but P2 prints 011, so it is wrong. 3 3 replyShare Rajesh Panwar commented Jun 5, 2019 reply Follow flag nice approach 0 0 replyShare King_in_the_north commented Jul 3, 2019 reply Follow flag Can some explain how P1 is giving output? since if(n/2) condition first check for 173 the 86 which will be false for 86 and 0 will printed first . correct me please. 1 1 replyShare sachin270895 commented Aug 26, 2020 reply Follow flag it is if(n/2) not if(n%2) if(86/2) will be if(43) which is true 0 0 replyShare ahthneeuhl commented Aug 5 reply Follow flag @Arjun sir this is best answer than selected one! 0 0 replyShare Please log in or register to add a comment.
6 6 votes ans is 3). As P1 prints 10101101 same as given code in question but P2 prints the output in reverse order. Laxmi answered Jan 26, 2015 Laxmi comment Share Follow 0 reply Please log in or register to add a comment.