-
Notifications
You must be signed in to change notification settings - Fork 3
/
57thProgram_Example_13.cpp
64 lines (62 loc) · 1.57 KB
/
57thProgram_Example_13.cpp
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
54
55
56
57
58
59
60
61
62
63
64
/*********
* Least Common Factor/ Least Common Multiple/ :LCM →
* 16 & 8 → 2 x 2 x 2 x 2 x 1 = 16
* Hence LCM(16,8) = 16
*
* LCM = (a*b)/GCD(a,b) [Formula]
* *******************/
#include<iostream>
using namespace std;
int main(){
int a,b, smallest, hcf, lcm;
cout<<"Enter two numbers :";
cin>>a>>b;
//find smallest among two numbers
if(a>b){
smallest = b;
}
else{
smallest = a;
}
//find GCD of two numbers
for(int i=1;i<=smallest;i++){
if(a%i==0 && b%i==0){
hcf = i;
}
}
lcm = (a*b)/hcf;
cout<<"LCM of "<<a<<" and "<<b<<" is "<<lcm;
return 0;
}
/****************************************************************
* Workings:
* ---------------------------------------------------------------
* a = 16; b = 8
* smallest = 8
* for i = 1 to 8 :
* when i = 1 :
* if(16%1==0 && 8%1==0) == True , hence hcf = 1
* when i = 2 :
* if(16%2==0 && 8%2==0) == True , hence hcf = 2
*
* when i = 3 :
* if(16%3==1 && 8%3==2) == False ,
* when i = 4 :
* if(16%4==0 && 8%4==0) == True , hence hcf = 4
* when i = 5 :
* if(16%5==1 && 8%5==3) == False ,
* when i = 6 :
* if(16%6==4 && 8%6==1) == False ,
* when i = 7 :
* if(16%7==2 && 8%7==1) == False ,
* when i = 8 :
* if(16%8==0 && 8%8==0) == True , hence hcf = 8
*
* Hence hcf = 8
*
* lcm = (a*b)/GCD(a,b) → (16 * 8)/ 8 = 16
*
*
*
*
* ***************************************************************/