fun(int x)
{
if(x > 0)
fun(x/2);
printf("%d", x);
}

above function is called as:
fun(10);

what will it print?



}




Answers were Sorted based on User's Feedback



fun(int x) { if(x > 0) fun(x/2); printf("%d", x); } above function is call..

Answer / guest

0 1 2 5 10

Is This Answer Correct ?    23 Yes 3 No

fun(int x) { if(x > 0) fun(x/2); printf("%d", x); } above function is call..

Answer / mahesh patil

Only answer 1
answer 4
answer 7 are correct others are wrong..
If you are confident on your answers please check once then
write a post.

Correct Answer: 0 1 2 5 10

This will print in reverse order because, This is a
recursive call, Every time a function is called the values
are stored in stack/stack is created. when x value reaches
0 then it will return. So stack is LIFO order, So it will
print the values in reverse order.

Is This Answer Correct ?    9 Yes 0 No

fun(int x) { if(x > 0) fun(x/2); printf("%d", x); } above function is call..

Answer / raj

0 1 2 5 10...Please don't post the wrong answer.

Is This Answer Correct ?    10 Yes 2 No




fun(int x) { if(x > 0) fun(x/2); printf("%d", x); } above function is call..

Answer / gg

0 1 2 5 10 is the answer. But can anybody explain why the
printing order 0 1 2 5 10. Why not 10 5 2 1 0 ? please...

Is it depends on stack allocation??

Is This Answer Correct ?    5 Yes 0 No

fun(int x) { if(x > 0) fun(x/2); printf("%d", x); } above function is call..

Answer / code

Guest & Raj is correct.......
Please don't post the wrong answer if u r not clear about this

Is This Answer Correct ?    4 Yes 1 No

fun(int x) { if(x > 0) fun(x/2); printf("%d", x); } above function is call..

Answer / sridhara bd

0 1 2 5 10

Is This Answer Correct ?    3 Yes 0 No

fun(int x) { if(x > 0) fun(x/2); printf("%d", x); } above function is call..

Answer / amit

All are incorrect. Please try it on a machine and see...
The answer is 012510. Please not that it is 0 1 2 5 10 but
without spaces.

Is This Answer Correct ?    1 Yes 0 No

fun(int x) { if(x > 0) fun(x/2); printf("%d", x); } above function is call..

Answer / hemavathiarun

Hi all,

since the code is calling the same function with different
values,it's not at all possible to move to printf statement
until x becomes < 0

so only when the compiler gets the value of x as 0 the loop
will be stopped.

Is This Answer Correct ?    1 Yes 0 No

fun(int x) { if(x > 0) fun(x/2); printf("%d", x); } above function is call..

Answer / sunil v r

5,2,1

Is This Answer Correct ?    1 Yes 0 No

fun(int x) { if(x > 0) fun(x/2); printf("%d", x); } above function is call..

Answer / ashwin kumar

hi Gg

answer is 0 1 2 5 10

this not stack prlm dear

here printf is after the function call dear so it is
printing 0 1 2 5 10

if u wnt to see 10 5 2 1 0 as output plz keep printf
function before function call that is

fun(int x)
{
if(x > 0)
printf("%d\n", x);
fun(x/2);

}

but output will be 10 5 2 1 only on 0 is printed

this above new code will give segmentation error in netbeans

thank u dear

Is This Answer Correct ?    0 Yes 0 No

Post New Answer



More C Interview Questions

a.One Cannot Take the address of a Bit Field b.bit fields cannot be arrayed c.Bit-Fields are machine Dependant d.Bit-fields cannot be declared as static Which of the Following Statements are true w.r.t Bit-Fields A)a,b&c B)Only a & b C)Only c D)All

3 Answers   Accenture, Digg.com,


1) There is a singing competition for children going to be conducted at a local club. Parents have been asked to arrive at least an hour before and register their children’s names with the Program Manager. Whenever a participant registers, the Program Manager has to position the name of the person in a list in alphabet order. Write a program to help the Program Manager do this by placing the name in the right place each time the Program Manger enters a name. 2) the Event Manager has to send participants to the stage to perform in the order in which they registered. Write a program that will help the Event Manager know who to call to the stage to perform. The Logic should be in Data Structures

0 Answers   KPIT,


A c program to display count values from 0 to 100 and flash each digit for a secong.reset the counter after it reaches 100.use for loop,. pls guys hepl me.. :(

0 Answers  


what is the output of the following program? #include<stdio.h> void main() { int x=4,y=3,z; z=x-- -y; printf("\n%d %d %d",x,y,z); }

14 Answers  


write a program to generate 1st n fibonacci prime number

11 Answers  






if a five digit number is input through the keyboard, write a program to calculate the sum of its digits. (hint:-use the modulus operator.'%')

23 Answers  


i want to make a program in which we use input having four digits(4321) and get output its reciprocal(1234).

1 Answers  


how would a 4*3 array A[4][3] stored in Row Major Order?

0 Answers   HCL, Ignou,


Switch (i) i=1; case 1 i++; case 2 ++i; break; case 3 --i; Output of i after executing the program

5 Answers   Mascot,


write an algorithm to display a square matrix.

0 Answers  


write a program wch produces its own source code aas its output?

1 Answers   IonIdea,


void main() { //char ch; unsigned char ch; clrscr(); for(ch =0;ch<= 127; ch++) printf(" %c= %d \t ", ch, ch); } output?

4 Answers   Groupon,






Categories