Example 36: Find LCM and GCD
Learn how to calculate LCM and GCD using functions.
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.
Press + to interact
#include <stdio.h>// try to implement a code that computes the LCM of the given two numbers.int lcm (int n1, int n2){int z = 0;// write your code herereturn z;}
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.
Press + to interact
#include <stdio.h>// try to implement a code that computes the GCD of the given two numbers.int gcd (int n1, int n2){int z = 0;// write your code herereturn z;}
Solution
...Access this course and 1400+ top-rated courses and projects.