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..
Here is your code:
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.