greatest common divisor is a prime number (pairs of numbers problem in C)
I have to build a program in C who reads a n integer number and n pairs of 2 integers numbers.The program should display those pairs of numbers I read whose greatest common divisor is a prime number.
My problem is that I get stuck at the code for reading the pairs of numbers and how to print them if they respect the condition. My code:
Code:
#include<stdio.h>
// find the gcd between 2 numbers
int gcd(int a,int b)
{
if(a==0)
return b;
while(b!=0)
{
if(a>b)
{
a=a-b;
}
else
{
b=b-a;
}
}
return a;
}
// check if a number is prime
int prime(int a)
{
int i;
if(a<2)
return0;
for(i=2;i<=a-1;i++)
{
if(a%i==0)
return0;
}
return1;
}
int main()
{
int n,i;
int x,y,result;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d%d",&x,&y);
printf("(%d,%d)",x,y);
result=gcd(x,y);
}
for(i=0;i<n;i++)
{
if(prime(result)==1)
printf("(%d,%d)",x,y);
}
return0;
}
My problem is in main function where I get stuck at this part of reading and displaying the pairs.For example if I enter n=3 and I read the following pairs: (12,20),(3,6) and (14,21) the program should display (3,6) and (14,21) because gcd of these pairs is a prime number.