686 views
2 votes
2 votes

Consider the following program

int find (int n)
{
  int a = 1;
  for (i = 1; i < = n; i ++)
     for (j = 1; j < = i; j++)
        for (k = 1; k <= j, k++)
            a = a + 1;                                 
  return a;
}

The value returned by find (9) is ______ .

2 Answers

Best answer
3 votes
3 votes

$\sum_{i=1}^{n}\sum_{j=1}^{i}\sum_{k=1}^{j}1 = \sum_{i=1}^{n}\sum_{j=1}^{i}j = \sum_{i=1}^{n}(i*(i+1)) /2 =    1+3+6+10+15+21+28+36+45 = 165$

Now, initial value of a was 1. Now a value incremented 165 times by 1. So final ans = 1+165 = 166

//https://ide.geeksforgeeks.org/bo7T4PnEfn (If you want to check)

selected by
0 votes
0 votes
Nested For loop:

I=1     I=2      I=3      I=4……………………………  I=8    I=9

J=1     J=2     J=3     J=4…………………………… J=8   J=9

K=1    K=2    K=6      K=10………………………… K=36  K=45

 

Sum of all these you will get: 165

So now value of a increasing linearly with variable K. Now a=165+1 =166

Ans: 166

Related questions

2 votes
2 votes
2 answers
1
Parshu gate asked Nov 12, 2017
513 views
int i = 0 ;main( ){printf ( "\nmain's i = %d", i ) ;i++ ;val( ) ;printf ( "\nmain's i = %d", i ) ;val( ) ;}val( ){i = 100 ;printf ( "\nval's i = %d", i ) ;i++ ;}
0 votes
0 votes
0 answers
2
Na462 asked Aug 22, 2018
479 views
What will be output ?A. Abnormal Termination.B. Infinite loopC. Output wil be 65536D. NoneAns. D
0 votes
0 votes
0 answers
4
srestha asked Mar 7, 2018
603 views
#include<stdio.h int fun1(int x) { x=14; } int fun2(int x) { x=20; } int main() { int(*p)(),m,n; scanf("%d",&m); if(m) p=fun1; else p=fun2; n=(*p)(); printf("%d",n); retu...