Created
February 3, 2020 16:35
-
-
Save Anan5a/e22c7e34bccc6d3b1bd97edc08537ae4 to your computer and use it in GitHub Desktop.
Calculate GCD of two number using loop
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
#include <stdio.h> | |
//Anan5a<https://github.com/Anan5a> | |
int main() | |
{ | |
int db[1000], da[1000], i, j=1, k, gcd[300], big_da, big_db; | |
int num, num2; | |
printf("Enter two numbers: "); | |
scanf("%d %d", &num, &num2); | |
for(i=1;i<=num;i++){ | |
if(num%i == 0){ | |
db[j] = i; | |
j++; | |
} | |
} | |
db[0] = j;//set max iteration | |
j = 1; | |
for(i=1;i<=num2;i++){ | |
if(num2%i == 0){ | |
da[j] = i; | |
j++; | |
} | |
} | |
da[0] = j;//set max iteration | |
j = 1; | |
//gcd[1] = da[0]; | |
for(i=1;i<da[0];i++){ | |
for(k=1;k<db[0];k++){ | |
if(da[i] == db[k]){ | |
gcd[j] = da[i]; | |
j++; | |
} | |
} | |
} | |
gcd[0] = j; | |
big_da = gcd[1]; | |
for(i=1;i<gcd[0];i++){ | |
if(gcd[i] > big_da){ | |
big_da = gcd[i]; | |
} | |
} | |
big_db = db[1]; | |
for(i=1;i<db[0];i++){ | |
if(db[i] > big_db){ | |
big_db = db[i]; | |
} | |
} | |
printf("%d", big_da); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment