Skip to content

Instantly share code, notes, and snippets.

@Anan5a
Created February 3, 2020 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Anan5a/e22c7e34bccc6d3b1bd97edc08537ae4 to your computer and use it in GitHub Desktop.
Save Anan5a/e22c7e34bccc6d3b1bd97edc08537ae4 to your computer and use it in GitHub Desktop.
Calculate GCD of two number using loop
#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