Im trying to find the GCD 2 separate ways, and the second one is by the Euclidian Algorithm. so far the first way seems acurate however for the second part I always get the out put 0. Here is my program, any help would be nice.
#include <iostream>
using namespace std;
int main()
{
int num1, num2, x, r,y,z;
cout << "first number:";
cin >> num1;
cout << "second number:";
cin >> num2;
{
if(num1 > num2) x = num2;
else if(num2 > num1) x = num1;
while (num1%x >0 || num2%x >0)
{
x--;
}
cout << "GCD ("<< num1<<","<<num2<<")= "<<x<<endl;
}
if(num1 > num2)
{
y = num2;
z = num1;
}
else
{
y = num1;
z=num2;
}
do
{
r=z%y;
if(r!=0)
{
z=y;
y=r;
}
}
while(r!=0);
cout<< "GCD through Euclidean Algorithm = "<<r<<endl;
return 0;
}
I figured it out, my output was showing up as zero because the loop ended when r=0, what i should have done is made my output z instead of r. Im leaving this here for anyone else who might need help with the Euclidean Algorithm