search
Log In
4 votes
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 ___________

in Programming 329 views

3 Answers

1 vote
 
Best answer
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

hence Answer is 3000

0
3000 is answer
0
Output will be 3000
0 votes
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 answer
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)$
asked May 19, 2019 in Programming srestha 340 views
1 vote
2 answers
2
246 views
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?$
asked May 12, 2019 in Programming srestha 246 views
1 vote
0 answers
3
221 views
Consider the following C code: int getNextGap(int gap){ gap=(gap*10)/13; if(gap<1)return 1; return gap; } void mystery(int a[ ],int n){ int gap=n; bool red=true; while(gap!=1||red==true){ gap=getNextGap(gap); red=false; for(int i=0;i<(n-gap ... n is number of array elements, then what output will print at last? I mostly stuck how bool function working here. Plz help me out, how program executing:(
asked May 10, 2019 in Programming srestha 221 views
1 vote
0 answers
4
229 views
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??
asked May 8, 2019 in Programming srestha 229 views
...