[C++] Greatest Common Factor Of Two Numbers
Here's a program that will ask for two numbers then give the greatest common factor.
Source Code:
Code:
#include <iostream.h>
void GCF(int x, int y)
{
int z, l=1;//i'm setting l and will use it as a loop
z=y+1;
while(l!=0)//keep looping until l is 0, which it wont be, unless common factors are found
{
z--; //subtracts from number
if(y%z == 0 && x%z == 0) // if remainders equal zero, set l to zero, so it can display that number
{
l=0;
}
}
cout<<"The GCF is: "<<z<<endl; //z is now the GCF
}
int main()
{
int x, y;
cout<<"Enter a number: ";
cin>>x;
cout<<"Enter another number: ";
cin>>y;
if(x>y) //if and else statements take care of different inputs, and call the void function above.
{
GCF(x, y);
}
else
{
GCF(y, x);
}
system ("PAUSE");
return 0;
}