-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGCDandLCM.cpp
More file actions
47 lines (39 loc) · 805 Bytes
/
GCDandLCM.cpp
File metadata and controls
47 lines (39 loc) · 805 Bytes
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
// Program to calculate GCD and LCM of two numbers.
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
int gcd(int x, int y)
{
int r = 0, a, b;
a = (x > y) ? x : y; // a is greater number
b = (x < y) ? x : y; // b is smaller number
r = b;
while(a % b != 0)
{
r = a % b;
a = b;
b = r;
}
return r;
}
int lcm(int x, int y)
{
int a;
a = (x > y) ? x : y; // a is greater number
while(true)
{
if(a % x == 0 && a % y == 0)
return a;
++a;
}
}
int main(int argc, char **argv)
{
cout<<"Enter the two numbers: ";
int x, y;
cin>>x>>y;
cout<<"The GCD of two numbers is: "<<gcd(x, y)<<endl;
cout<<"The LCM of two numbers is: "<<lcm(x, y)<<endl;
return 0;
}