Skip to content

Commit 92226b2

Browse files
Add files via upload
Euclid's Algorithm is helpful when there is need to get the hcf that is greatest common divisor or highest common factor so in that case we use the algorithm.....
1 parent 9ea7653 commit 92226b2

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

19_euclidsalgorithm.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <iostream>
2+
using namespace std;
3+
int greatestCommonDivisor(int a,int b){
4+
if(a==0){
5+
return a;
6+
}
7+
else if(b==0){
8+
return b;
9+
}
10+
else{
11+
while(a!=b){
12+
if(a>b){
13+
a = a-b;
14+
}
15+
else{
16+
b=b-a;
17+
}
18+
}
19+
}
20+
return a;
21+
}
22+
int LCM(int a,int b){
23+
return (a*b)/greatestCommonDivisor(a,b);
24+
}
25+
int main(){
26+
// Euclid's algorithm is basically derived to find the gcd of two numbers that is highest common factor or greatest common divisor.....
27+
int a,b;
28+
cout<<"Enter the first number: ";
29+
cin>>a;
30+
cout<<"Enter the second number: ";
31+
cin>>b;
32+
33+
int gcd = greatestCommonDivisor(a,b);
34+
cout<<endl<<"GCD is: "<<gcd<<endl;
35+
cout<<"LCM is: "<<LCM(a,b)<<endl;
36+
37+
38+
return 0;
39+
}

19_euclidsalgorithm.exe

44.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)