Code:void gcd_lcm(int factor1, int factor2, int *gcd, int *lcm) { *gcd = factor1 % factor2; if (gcd > 0) { gcd_lcm(factor1 = factor2, factor2 = *gcd, gcd, lcm); } }
Before we get into the code, lets look into the algorithm. How exactly do you calculate the gcd? the lcm?
Lets look at what your algorithm does with gcd(3,6), gcd(6,3), and gcd(6, 4)
gcd(6,3)
1) *gcd = 0
2) !(0 > 0) -> done. (Well, that's not right)
gcd(3,6):
1) *gcd = 3
2) 3 > 0
3) gcd(6,3) => *gcd = 0
gcd(6,4)
1) *gcd = 2
2) 2 > 0
3) gcd(4,2)
4) *gcd = 0
5) !(0 > 0) => *gcd = 0