Example 36: Find LCM and GCD
Explore how to implement functions in C to compute the least common multiple (LCM) and greatest common divisor (GCD) of two integers. This lesson guides you through writing modular code with lcm() and gcd() functions, improving problem-solving skills and understanding of function usage in C.
We'll cover the following...
We'll cover the following...
Problem
Write a program that takes two integers as input and obtains LCM and GCD of them through the functions lcm() and gcd().
Examples
| Input | Output (LCM) | Output (GCD) |
|---|---|---|
| 4 , 5 | 20 | 1 |
| 3 , 12 | 12 | 3 |
Try it yourself - LCM
Try to solve the LCM problem on your own in the code widget below. If you get stuck, you can always refer to the solution provided.
Try it yourself - GCD
Try to solve the GCD problem on your own in the code widget below. If you get stuck, you can always refer to the solution provided.
Solution
Given ...