# Greatest Common Factor

• 11-27-2005
cppdungeon
Greatest Common Factor
This is my probem: my code should work. in fact, for small numbers it does. (like 2 and 2). but it isnt working, and it gives me one of those "microsoft error" things. please help me.

Code:

```#include <iostream> #include <math.h> #include <string.h> using namespace std; int main(){     int a, b, c, x, y, z;     int big [100];     int small [100]; z=1;                 cout<<"what are the two numbers?\n";                 cin>>a>>b;                 if(a>b){                 x=a;                 }                 if(a<b){                 x=b;                 }                 for(; x<100;x--){                 if((a%x==0)&&(b%x==0)&&(x>z)){                 z=x;                 }                 }                 cout<<z;                                                 cin.get();                 cin.get();                 }```
• 11-27-2005
The Brain
Consider the domain of integers you need to test in order to obtain the GCF between two numbers... determine the smaller of the two user entered numbers, and test all positive integers until you have tested the entire range of posibilities, from '2' to the lesser of the two user entries.

In your loop condition, you arbitrarily test the two numbers for a GCF using a range of integers from the largest user entered integer to negitive infinity (if 'x' is less than or equal to 100) and from x to 100 (if 'x' is greater than 100)... but what if the two integers were like.. 10 and 15 for example? in which case, all you need to do is test a range of integers from 2 to 10 inclusive.

Code:

```cin >> a;  // a == 10 in this example cin >> b;  // b == 15 in this example```
which brings us to this:
Code:

```if(a<b){                 x=b;    // x == 15      I think the smaller of the two number should set the loop condition..  not the largest```

and finally, we are brought to the main algorithm.. a loop that should find a GCF if one exists..

Code:

```for(; x<100;x--){                  if((a%x==0)&&(b%x==0)&&(x>z)){                 z=x;                 }```

I think this would be better logic for your loop condition:
Code:

```z=0; for(; x>1;x--){  //where 'x' == the lesser of two user entered integers                 if((a%x==0)&&(b%x==0)&&(x>z)){                 z=x;```

tips: Look for ways to break out of the loop as soon as the GCF is detected. Also, make sure your program can handle a situation where the two user entered integers are the same.. and cases where no GCF exists.
• 11-27-2005
Salem
Man, there's some ugly indenting going on in this thread...
• 11-27-2005
cppdungeon
Thanks!!! i got it wo work finaly. here is the completed code:

Code:

```#include <iostream> #include <math.h> #include <string.h> using namespace std; int main(){     int a, b, c, x, y, z;     int big [100];     int small [100]; z=1;                 cout<<"what are the two numbers?\n";                 cin>>a>>b;                 if(a>b){                 x=b;                         }                 if(b>a){                 x=a;                            }                 if(a==b){                 z=a;                 }                 if(a!=b){                 for(  ;x>0  ;x--  ){                 if(a%x==0&&b%x==0&&x>z){                 z=x;                 }                 }                        }                                 cout<<z;                                                 cin.get();                 cin.get();                 }```
• 11-28-2005
nvoigt
As the formatting remark was reported as "rude, random and unnessecary" I'll post the "nice, targetted and neccessary" version: This code looks like someone randomly sprinkled breaks and new lines over the code. For good, easily understandable and maintainable code, it IS a neccessity to properly format it. The sooner you learn to format in a readable manner, the sooner will you understand your and other peoples programs.

Just to give an example of code that was formatted and cleaned:

PHP Code:

``` #include <iostream> using namespace std; int main() {     int a, b, x, z;     z = 1;     cout << "what are the two numbers?" << endl;     cin >> a >> b;     if( a > b )     {         x = b;     }     if( b > a )     {         x = a;          }          if( a == b )     {         z = a;     }          if( a != b )     {         for(  ; x > 0 ; x-- )         {             if( ( a % x == 0 ) && ( b % x == 0 ) && ( x > z ) )             {                 z = x;             }         }             }            cout << z;           cin.get();     cin.get();     return 0; }  ```
• 11-28-2005
Salem
I see the OP hasn't improved much either.