Problem Statement
write a c program to do the following
write a c program to do the following
- Read in an integer n
- print out all pairs of co-primes in the integers 2 to n.
#include<stdio.h>
int gcd(int i,int j);
int main()
{
int i,n,j,y;
printf("enter the value of n \n");
scanf("%d",&n);
for(i=2;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
y=gcd(i,j);
if(y==1)
{
printf(" {%d,%d} ",i,j);
}
}
}
printf("\n");
return 0 ;
}
int gcd(int i,int j) /* j will be greater always */
{
int temp;
while(j%i!=0)
{
temp=j%i;
j=i;
i=temp;
}
return(i);
}
No comments:
Post a Comment