The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
0 votes
69 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 ___________

asked in Programming by Veteran (111k points) | 69 views

2 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.

answered by Boss (12.9k points)
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

answered by (83 points)

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
49,540 questions
54,099 answers
187,269 comments
71,006 users