in Programming
108 views
0 votes
0 votes
What will be the output printed for find(4)?

void find(int x)

{

          static int i = 10, y = 0;

          y = y + i;

          for(i; i>0; i = i - 10)

          {

                if(x! = 0)

                   find(x – 1);

               else

                   printf(“%d”, y);

          }

}
in Programming
108 views

2 Comments

thanks
0
0

2 Answers

2 votes
2 votes

ANS → Y=50 .

As here variable i and y both are static so all the updations will be saved in it.

void find(int x)

{        static int i = 10, y = 0  ;      //LINE 1
              y = y + i;                          //LINE 2

}
 

Now in LINE 1  i=10 and y=0

            LINE 2  y=y+10  // y=10

  for(i; i>0; i = i - 10)

          {

                if(x! = 0)

                   find(x – 1);

               else

                   printf(“%d”, y);

          }

in the first iteration of this loop .

i=10 , y=10   // it will enter the loop

x=4 and x!=0

thus go inside if and find(x-1)  i.e  find(3)

so for find(3) →   y=y+i again  i.e  y=10+10 // 20

then again for  find(2) →       y=y+i again  i.e  y=20+10 // 30

then  find(1) →       y=y+i again  i.e  y=30+10 // 40

find(0) →       y=y+i again  i.e  y=40+10 // 50

as now x =0 then it will not enter the if condition and directly prints  y i.e 50.

i hope it is cleared now!!

edited by

3 Comments

can you please elaborate how 50 came ???
0
0

after first iteration i=0,then how can run 2nd itration coz for loop fail 

0
0

before exiting the for loop  a recursive call is there  so before updating the value of i=0 all the recursive calls are completed first and thus only one loop is there for each recursive call.

 

0
0
0 votes
0 votes
y=10