Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 48984d1

Browse files
committed
Worked on [Algorithm] Euclidean GCD [C] fnplus#144
1 parent 093eb43 commit 48984d1

File tree

1 file changed

+45
-0
lines changed
  • Algorithms/Maths/Euclidean GCD/C

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <stdio.h>
2+
3+
//Recursive version of GCD
4+
int gcd_recursive(int num1, int num2)
5+
{
6+
if (num2 == 0)
7+
return num1;
8+
return gcd_recursive(num2, num1%num2);
9+
}
10+
11+
//Non Recursive version of GCD
12+
int gcd_non_recursive(int num1, int num2)
13+
{
14+
if(num1 < num2)
15+
{
16+
int temp = num1;
17+
num1 = num2;
18+
num2 = temp;
19+
}
20+
while(num2!=0)
21+
{
22+
int temp = num2;
23+
num2 = num1 % num2;
24+
num1 = temp;
25+
}
26+
return num1;
27+
}
28+
29+
// Entry point
30+
int main()
31+
{
32+
int a = 10, b = 15;
33+
printf("Recursive-GCD(%d, %d) = %d\n", a, b, gcd_recursive(a, b));
34+
printf("Non-Recurivse-GCD(%d, %d) = %d\n", a, b, gcd_non_recursive(a, b));
35+
a = 35, b = 10;
36+
printf("Recursive-GCD(%d, %d) = %d\n", a, b, gcd_recursive(a, b));
37+
printf("Non-Recurivse-GCD(%d, %d) = %d\n", a, b, gcd_non_recursive(a, b));
38+
a = 31, b = 2;
39+
printf("Recursive-GCD(%d, %d) = %d\n", a, b, gcd_recursive(a, b));
40+
printf("Non-Recurivse-GCD(%d, %d) = %d\n", a, b, gcd_non_recursive(a, b));
41+
a = 10, b = 0;
42+
printf("Recursive-GCD(%d, %d) = %d\n", a, b, gcd_recursive(a, b));
43+
printf("Non-Recurivse-GCD(%d, %d) = %d\n", a, b, gcd_non_recursive(a, b));
44+
return 0;
45+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /