edited by
394 views

2 Answers

3 votes
3 votes

Yes it is given to be wrong..Let us see how..

When i = 1 and j = 1 , P will be printed 0 times as the if condition fails as 1 < 1 is false and the if conditions in subsequent instances of inner loop will also fail obviously as j increases..

When i = 2 , P gets printed for j = 1 only so one time printed

When i = 3 , P gets printed for j = 1,2 only so 2 times printed.

Now proceeding in this manner till 

When i = n , P gets printed for j = 1,2,3....,n-1 so (n-1) times printed..

Now from i = n + 1 to i = 2n - 1 , we will get to print j for n times..

So no of such i's  =  (2n - 1) - (n+1) + 1

                          =   n - 1

So total number of times P gets printed = (0+1+2....n-1 terms) + n * (n-1) 

                                                          = n(n-1)/2 + n(n-1)

                                                          =  3/2(n2 - n)

But none of the answers given so..So options should be corrected.. 

2 votes
2 votes

For i = 1 to n.

P1 = 1 + 2 + 3 + 4 + ..... + (n-1).

    = n(n-1)/2. 

For i = n+1 to i = 2n-1.

P2 = n(n-1).

P  = P1 + P2    

    =$\frac{ n(n-1)}2{} + n(n-1)$

   = $\frac{3n(n-1)}{2} = \frac{3(n^{2}-n)}{2}$

edited by

Related questions

0 votes
0 votes
1 answer
1
SSR17 asked Feb 29
207 views
#include <stdio.h int main() { int i = -1; int x = (unsigned char)i; printf("%d", x); return 0; }output is 255 , but please explain how
2 votes
2 votes
1 answer
4
rupamsardar asked Aug 30, 2023
468 views
#include <stdio.h int f(int x) { if(x%2==0) { return f(f(x-1)); } else return (x++); } int main() { printf("%d",f(12)); ret...