Created
July 4, 2023 10:04
-
-
Save AmirMahdyJebreily/84c4778eb2816e444790285f27409e5f to your computer and use it in GitHub Desktop.
Compute GDC in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int num1 = int.Parse(Console.ReadLine()); | |
int num2 = int.Parse(Console.ReadLine()); | |
Console.WriteLine(GCD(num1, num2)); | |
int GCD(int num1, int num2) | |
{ | |
int res = 0; | |
if (num1 == 0 || num2 == 0) | |
{ | |
res = 0; | |
} | |
else | |
{ | |
if (num1 > num2) | |
{ | |
for (int i = num2; i > 0; i--) | |
{ | |
if (num1 % i == 0 && num2 % i == 0) | |
{ | |
res = i; | |
break; | |
} | |
} | |
} | |
else if (num1 < num2) | |
{ | |
for (int i = num1; i > 0; i--) | |
{ | |
if (num1 % i == 0 && num2 % i == 0) | |
{ | |
res = i; | |
break; | |
} | |
} | |
} | |
else | |
{ | |
// a == b | |
res = num1; | |
} | |
} | |
return res; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment