329 views
#include<stdio.h>
#include<iostream>

int bar(int m, int n){
if(m==0)return n;
if(n==0)return m;
return bar(n%m,m);
}

int foo(int m,int n){
return(m*n/bar(m,n));
}

int main(){
int x=foo(1000,1500);
printf("%d",x);
return 0;
}

Output of the program is ___________

1 vote

int x=foo(1000,1500); // this line in main called foo

foo(1000,1500) $\rightarrow$ bar(1000,1500) $\rightarrow$ bar(500,1000) $\rightarrow$ bar(0,500)

now coming bakwards from the recursive stack

bar(0,500) returns n= 500 to bar(0,500)

bar(0,500) returns 500 to bar(500,1000)

bar(500,1000) returns 500 to bar (1000,1500)

bar(1000,1500) returns 500 to foo(1000,1500)

Now in foo

return(m*n/bar(m,n));

becomes

return(1000*1500/500); = return(1000,*3) = return 3000

so foo(1000,1500) returns 3000 to x in main()

printf("%d",x); // prints 3000.

$\therefore$ Output is 3000.

edited by
0
Yes bar will output 500 on simplify it would give 3000
1 vote

x=foo(1000,1500)

foo()=1500000/bar(1000,1500)

foo()=1500000/bar(500,1000)

foo()=1500000/bar(0,500)

foo()=1500000/500=3000  => x=3000

0
0
Output will be 3000
You need not check for such large values just take m=10 and n=15. You will find that the value returned by bar(m,n) is 5.

m*n here is equal to 150 when you divide 150 by 5 you get 30 . you just need to add 2 additional zeros (m=10*100 and n=15*100)to get the result i.e., 3000

## Related questions

1 vote
1
340 views
Consider the following $C$ implementation which when given $3$ numbers a,b,c as input, find the maximum of $3$ numbers $a,b,c.$ int kickstart(int a,int b,int c) { if(B1) return a; if(a>=b) return B2; return kickstart(c,a,b); } How the boxes filled up correctly? $I)B1:a\geq b$ ... $IV)B1:a\geq b$ && $a\geq c, B2:kickstart\left ( b,c,a \right );$ Is it $I) and II)$ or $I) and IV)$
1 vote
Consider the following function $foo()$ void foo(int n){ if(n<=0) printf("Bye"); else{ printf("Hi"); foo(n-3); printf("Hi"); foo(n-1); } } Let $P(n)$ represent recurrence relation, indicating number of time print statement executed. What will best recurrence for ... $n=0$ The options are confusing to me. Can someone explain the options well. Moreover , what will be constant added $1$ or $2?$
Consider the following function int fun(int a[ ],int l, int target){ int i=0,j=0,sum=0,count=0; while(j<l){ if(sum<target){ sum=sum+a[j]; j++; } else if(sum>target){ sum=sum-a[i]; i++; } else{ count++; sum=sum-a[i]; i++; } } if(sum==target) ... be return value of function call $fun\left ( a,16,8 \right )=$_______________ Given ans $6,$ but I got $4.$ Which one correct?? Any shortcut to evaluate??