3
\$\begingroup\$

I want to eliminate code duplication in my Fizz-Buzz like program. Any ideas?

#include <stdio.h>
int myseries(int n) {
  int i, cpt = 0;
  if (n < 0) {
    for (i = 0; i >= n; i--) {
      // if the number is multiple of both three and five
      if (i % 15 == 0) {
        printf("lancelot\n");
      }
      // if the number is multiple of 3
      else if(i % 3 == 0) {
        printf("Fizz\n");
      }
      // if the number is multiple of 5
      else if(i % 5 == 0) {
        printf("Buzz\n");
        cpt++;
      }
      else {
        printf("%d\n", i);
      }
    }
    return cpt;
  }
  else {
    for (i = 0; i <= n; i++) {
      // if the number is multiple of both three and five
      if (i % 15 == 0) {
        printf("lancelot\n");
      }
      // if the number is multiple of 3
      else if(i % 3 == 0) {
        printf("Fizz\n");
      }
      //if the number is multiple of 5
      else if(i % 5 == 0) {
        printf("Buzz\n");
        cpt++;
      }
      else {
        printf("%d\n",i);
      }
    }
    return cpt;
  }
}
//example
main() {
  printf("the number of buzz is : %d", myseries(-16));
}
\$\endgroup\$
3
\$\begingroup\$

Move common code to function

The first thing you could do to eliminate duplication is to take the common code and move it to a function. In your case, the result would look like this:

// Returns 1 for buzz, 0 for any other.
int handleNum(int num)
{
    // if the number is multiple of both three and five
    if (num % 15 == 0) {
        printf("lancelot\n");
    }
    // if the number is multiple of 3
    else if(num % 3 == 0) {
        printf("Fizz\n");
    }
    // if the number is multiple of 5
    else if(num % 5 == 0) {
        printf("Buzz\n");
        return 1;
    }
    else {
        printf("%d\n", num);
    }
    return 0;
}

int myseries(int n) {
    int i, cpt = 0;
    if (n < 0) {
        for (i = 0; i >= n; i--) {
            cpt += handleNum(i);
        }
    }
    else {
        for (i = 0; i <= n; i++) {
            cpt += handleNum(i);
        }
    }
    return cpt;
}

Handle negative and positive alike

The next thing you could do is to combine the negative and positive cases into the same loop. Both loops iterate the same number of times, just in different directions. So you could just remember whether the original number was negative, and then iterate only in the positive direction, like this:

int myseries(int n) {
    int i, cpt = 0;
    int numberSign = (n < 0) ? -1 : 1;

    n *= numberSign;
    for (i = 0; i <= n; i++) {
        cpt += handleNum(i * numberSign);
    }
    return cpt;
}

Other things

Your indentation level of 2 is too small for my eyes. I had to increase the indentation to 4 to properly distinguish the different levels.

When you print the result in main(), you should print a newline as well. When I ran your program, my shell prompt was put at the end of your output.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.