in Programming
788 views
4 votes
4 votes
#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
by
788 views

3 Answers

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

1 comment

Yes bar will output 500 on simplify it would give 3000
1
1
1 vote
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

2 Comments

moved by
3000 is answer
0
0
moved by
Output will be 3000
0
0
0 votes
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