25 votes 25 votes What is the output printed by the following program? #include <stdio.h> int f(int n, int k) { if (n == 0) return 0; else if (n % 2) return f(n/2, 2*k) + k; else return f(n/2, 2*k) - k; } int main () { printf("%d", f(20, 1)); return 0; } $5$ $8$ $9$ $20$ Algorithms gateit-2005 algorithms identify-function normal + – Ishrat Jahan asked Nov 3, 2014 • edited Jun 12, 2018 by Milicevic3306 Ishrat Jahan 9.1k views answer comment Share Follow See all 2 Comments See all 2 2 Comments reply bthebestSelf commented Dec 26, 2020 reply Follow Share Answer should be -15 which is not given in the options, observe carefully the value of k 0 votes 0 votes Yaman Sahu commented Nov 13, 2021 i edited by Yaman Sahu Nov 13, 2021 reply Follow Share int f(int n, int k) { if (n == 0) return 0; else if (n % 2) return f(n/2, 2*k) + k; // This condition is true when n%2 is 1 else return f(n/2, 2*k) - k; // otherwise } bthebestSelf you are doing n%2 = 0 as true, then you get -15 as the answer. If you doing n%2 = 0 as false then you get 9 as an answer :) 6 votes 6 votes Please log in or register to add a comment.
Best answer 34 votes 34 votes See the following calling sequence. Boxed values show the return values. Hence, answer is option C. Rajesh Pradhan answered Nov 13, 2016 • edited May 7, 2021 by gatecse Rajesh Pradhan comment Share Follow See all 5 Comments See all 5 5 Comments reply Show 2 previous comments Vamsi krishna satya commented Dec 18, 2018 reply Follow Share it is if (n % 2) return f(n/2, 2*k) + k which means n%2 must be 1 for condition to be true.I think you mistook it for if (n%2==0) 9 votes 9 votes shashankrustagi commented Jan 14, 2021 reply Follow Share ya, its ok. you mistook, you will never mistook it agin. ok keep it up vamsi 0 votes 0 votes ritiksri8 commented 2 days ago reply Follow Share @Vamsi Krishna Satya I also mistook... 0 votes 0 votes Please log in or register to add a comment.
12 votes 12 votes The sequence has to be followed. 6.) f(20,1) = 9. 5.) f(10,2) - 1 = 9 4.) f(5,4) - 2 = 10 3.) f(2,8) + 4 = 12 2.) f(1,16) - 8 = 8 1.) f(0,32) + 16 = 16 Gate Keeda answered Nov 3, 2014 Gate Keeda comment Share Follow See all 7 Comments See all 7 7 Comments reply Neha Sisodiya commented Jul 14, 2015 reply Follow Share Why have u executed else part in 5th step I guess else if has to be executed since 10 which is divisible by 2. 0 votes 0 votes Arjun commented Jul 14, 2015 reply Follow Share else if (n%2) This will be true if n is NOT a multiple of 2. When n is a multiple of 2, n%2 returns 0 and if will be false. 11 votes 11 votes Neha Sisodiya commented Jul 15, 2015 reply Follow Share Thank you sir. 1 votes 1 votes neha singh commented Aug 24, 2016 reply Follow Share how values are come i mean like 9,10,8.,16 etc.Anybody plz explain? 0 votes 0 votes neha singh commented Aug 24, 2016 reply Follow Share Can anybody plzz explain how this series values comes. 0 votes 0 votes neha singh commented Aug 24, 2016 reply Follow Share its clear now. 0 votes 0 votes Sandeep Suri commented Jan 9, 2018 reply Follow Share Else if (n%2) when n is multiple of 2 it will be like else if (0) therefore it will return false and will execute next else and return f(n/2,2*k) - K.... 0 votes 0 votes Please log in or register to add a comment.
0 votes 0 votes 9 will be printed. Answer: C manish_pal_sunny answered Aug 17, 2020 manish_pal_sunny comment Share Follow See all 0 reply Please log in or register to add a comment.