-
Notifications
You must be signed in to change notification settings - Fork 0
/
GCD.CPP
70 lines (62 loc) · 879 Bytes
/
GCD.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
65
66
67
68
69
70
#include<iostream.h>
#include<conio.h>
int gcd1(int a,int b)
{
if(b==0)
{
return(a);
}
else
{
return gcd1(b,a%b);
}
}
void gcd2(int a,int b)
{
int i,m=1;
if(a<b)
{
for(i=1;i<=a;i++)
{
if(a%i==0&&b%i==0)
m=i;
}
}
else if(a>b)
{
for(i=1;i<=b;i++)
{
if(a%i==0&&b%i==0)
m=i;
}
}
else
m=a;
cout<<m;
}
void main()
{
int a,b,ch;
here:
clrscr();
cout<<"Choose (to find GCD of 2 nos., using):-\n\n"
<<"\n1) Recursion"
<<"\n2) Iteration\n";
cin>>ch;
clrscr();
cout<<"Enter 2 nos. of terms: ";
cin>>a>>b;
cout<<"\nGCD is: \n";
if(ch==1)
{
cout<<gcd1(a,b);
}
if(ch==2)
{
gcd2(a,b);
}
cout<<"\n\nEnter 1 to enter again, 2 to exit.";
cin>>ch;
if(ch==1)
goto here;
}