Co-Prime Pairs

Problem Statement
write a c program to do the following
  1. Read in an integer n
  2. print out all pairs of co-primes in the integers 2 to n. 
For example, if you enter 6, the output should look like (2,3) (2,5) (3,4) (3,5) (4,5) (5,6).

#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