-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombined.c
87 lines (75 loc) · 1.38 KB
/
combined.c
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <stdio.h>
#include <stdlib.h>
int euclidAlg(int a, int b)
{
int rem;
while (b != 0)
{
rem = a % b;
a = b;
b = rem;
}
return (a);
}
int min(int a, int b)
{
return a > b ? b : a;
}
int consecutiveInt(int a, int b)
{
if (a == 0)
return b;
if (b == 0)
return a;
int gcd = min(a, b);
while (gcd != 0)
{
if (a % gcd == 0)
{
if (b % gcd == 0)
{
return (gcd);
}
}
gcd--;
}
return (-1);
}
int middleSchool(int a, int b)
{
if (a == 0)
return b;
if (b == 0)
return a;
int i = 2, gcd = 1;
while (a >= i && b >= i)
{
if (a % i == 0)
{
if (b % i == 0)
{
gcd = gcd * i;
b = b / i;
}
a = a / i;
}
else
i++;
}
return gcd;
}
int main()
{
int a, b, gcd;
printf("Enter the first number: ");
scanf("%d", &a);
printf("Enter the second number: ");
scanf("%d", &b);
gcd = euclidAlg(a, b);
printf("\n1-GCD using Euclid's algorithm: %d", gcd);
gcd = consecutiveInt(a, b);
printf("\n2-GCD using consecutive integers: %d", gcd);
gcd = middleSchool(a, b);
printf("\n3-GCD using mid school method: %d", gcd);
return 0;
}