reopened by
496 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);

        }

}

 

reopened by

2 Answers

Best answer
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!!

selected by

Related questions

0 votes
0 votes
1 answer
1
Ray Tomlinson asked Aug 9, 2023
461 views
How many times is the comparison $i >= n$ performed in the following program?int i = 200 n = 80; main() { while (i >= n) { i = i - 2 n = n + 1 } }
0 votes
0 votes
1 answer
2
TusharKumar asked Dec 22, 2022
524 views
can anyone explain how the for loop works here..?