For a student course in c, I need to find the prime greatest common divisor (gcd) of two integers. If there is no answer the output should be 1. You can only use if statement, scanf, loops (no external functions).
Examples of inputs and outputs:
(20,20)--->5
(21,20)--->1
(22,20)--->2
(29,29)--->29
Can someone please help me with this? Here is what I have so far:
#include <stdio.h>
int main()
{
int num1, num2, i, hcf;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
for(i=1; i<=num1 || i<=num2; ++i)
{
if(num1%i==0 && num2%i==0) /* Checking whether i is a factor of both number */
hcf=i;
}
printf("gcd of %d and %d is %d", num1, num2, hcf);
return 0;
}
There are a lot of examples on how to find the gcd but none that I have found for the prime gcd.
sample of fix
#include <stdio.h>
int main(void){
int num1, num2, i, hcf = 1;
int tmp1, tmp2;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
tmp1 = num1;
tmp2 = num2;
for(i=2; i<= tmp1 && i<= tmp2; ++i){
while(tmp1 % i== 0 && tmp2 % i == 0){
hcf=i;
tmp1 /= hcf;
tmp2 /= hcf;
}
}
printf("gcd of %d and %d is %d", num1, num2, hcf);
return 0;
}