Final edit.
Code:
#include <stdio.h>
#include <stdbool.h>
int main()
{
int m, n, gcd, remainder;
bool isPrimeNum1 = true, isPrimeNum2 = true;
int d;
printf("Enter two integers. ");
scanf("%d %d", &m, &n);
// Check to see if we already have the gcd by checking if one of the integers is zero.
// The integer that is not zero is the gcd.
if(m == 0)
{
gcd = n;
printf("The greatest common divisor is: %d", gcd);
return 0;
}
else
{
gcd = m;
printf("The greatest common divisor is: %d", gcd);
return 0;
}
// Check to see if the first number is prime.
for(d = 2; d * d < m; d++)
{
if(m % d == 0)
{
isPrimeNum1 = false;
break;
}
}
// If the first number is not prime, check to see if the next number is prime.
if(!isPrimeNum1)
{
for(d = 2; d * d < n; d++)
{
if(n % d == 0)
{
isPrimeNum2 = false;
break;
}
}
}
// If either one them is prime, check to see if the gcd is either the prime number itself or 1.
if(isPrimeNum1 || isPrimeNum2)
{
if(n % m == 0)
{
gcd = m;
printf("The greatest common divisor is: %d", gcd);
return 0;
}
else if(m % n == 0)
{
gcd = n;
printf("The greatest common divisor is: %d", gcd);
return 0;
}
else
{
gcd = 1;
printf("The greatest common divisor is: %d", gcd);
return 0;
}
}
// If we get to this part, then we must find the GCD by dividing the larger number with the smaller number
// and dividing the next larger number by the remainder.
// Repeat the step until you get a remainder of zero
while(1)
{
remainder = m % n;
m = n;
n = remainder;
if(n == 0)
{
gcd = m;
break;
}
}
printf("The greatest common divisor is: %d", gcd);
return 0;
}