Write a program that will compute and print out all positive integers that are less than and relatively prime to the integer 351. Two numbers are relatively prime if their greatest common divisor is 1. You must call a gcd function.

so far i have this, it compiles and works but doesnt print out the correct output, any suggestions?

#include <stdio.h>

int gcd(int i, int x);
int main(int argc, char *argv[]){
  int i, x = 351;

  printf("351 is relatively prime to:\n");
  for(i = 1; i < 351; i++){

    if( gcd( i, x ) == 1) printf("%d\n", i);
  }

  return 0;
}

int gcd(int i, int x){

  if(x % i == 0) return( i );
  return( gcd( x % i, x ) );
}

Your gcd is not quite right. The recursive call should be gcd(x, i % x) .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.