-
-
Notifications
You must be signed in to change notification settings - Fork 486
Expand file tree
/
Copy pathLCM.dart
More file actions
53 lines (49 loc) · 1.01 KB
/
LCM.dart
File metadata and controls
53 lines (49 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//Title:Find LCM of two numbers.
//Author:Shawn
//Email:[email protected]
/*suppose we have two numbers a and b.
* Property: Since product of LCM and GCD of two numbers are
* equal to product of that number itself.
* i.e, LCM(a,b)*GCD(a,b)=a*b
* So,here we first find GCD of two numbers and using above
* property we find LCM of that two numbers.
*/
//Recursive function to return gcd of a and b
int gcd(int a, int b) {
if (a == 0) {
return b;
}
return gcd(b % a, a);
}
//Function to return LCM of two numbers
double lcm(int a, int b) {
return (a * b) / gcd(a, b);
}
//Driver program
void main() {
var a, b;
//Test case1:
a = 15;
b = 20;
//print the result
print(
"LCM of " +
a.toString() +
" and " +
b.toString() +
" is " +
lcm(a, b).toString(),
);
//Test case2:
a = 12;
b = 18;
//print the result
print(
"LCM of " +
a.toString() +
" and " +
b.toString() +
" is " +
lcm(a, b).toString(),
);
}