I am trying to create a program that finds the GCD of two numbers using the brute force...
So far i have this but my problem (I hope) is that i cant get the value of g into int main()
Here is my program. Any suggestions?

``````#include <cstdio>
#include "simpio.h"
#include "strlib.h"

int GCD(int x, int y)
{
int g;

g = x;
while (x % g != 0 || y % g != 0)
{
g--;
}
return(g);
}

int main()
{
int n1,n2;

printf("1st number = ");
scanf("%d",&n1);

printf("2nd number = ");
scanf("%d",&n2);

printf("The GCD of %d and %d is %d",n1,n2,GCD(g));

return 0;
system("pause");
}``````

## All 3 Replies

``printf("The GCD of %d and %d is %d",n1,n2,GCD(g));``

``printf("The GCD of %d and %d is %d",n1,n2,GCD(n1,n2));``

In main:

``````int result;

result = GCD( n1, n2);
printf("result = %d\n", result);``````

>> g = x ;
g has to be least of the 2 numbers rather than simply x for the code to be faster.
Why brute force?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.