Skip to content

Commit

Permalink
Added fizzBuzz.c written in C
Browse files Browse the repository at this point in the history
  • Loading branch information
chihau authored and 12meses12katas committed Apr 25, 2011
1 parent 413e89a commit eadb00d
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions cchau/fizzBuzz.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *itoa(int num, char buffer[]);
int contiene(int num, int numero);

int main(void) {
int i;

for(i = 1; i <= 100; i++) {
if((i % 3 == 0 || contiene(i, 3) == 1) && (i % 5 == 0 || contiene(i, 5) == 1)) {
printf("FizzBuzz\n");
} else if((i % 3 == 0 || contiene(i, 3) == 1) && (i % 5 != 0)) {
printf("Fizz\n");
} else if((i % 3 != 0) && (i % 5 == 0 || contiene(i, 5) == 1)) {
printf("Buzz\n");
} else {
printf("%d\n", i);
}
}

return 0;
}

char *itoa(int num, char buffer[]) {
sprintf(buffer, "%d", num);

return buffer;
}

// chequea si un string contiene o no un num
// si lo contiene devuelve 1 sino devuelve -1
int contiene(int num, int numero) {
char buffer[4];
itoa(num, buffer);
int result =-1;
int i;
for(i = 0; i < strlen(buffer); i++) {
if((buffer[i] - '0') == numero) {
result = 1;
break;
}
}
return result;
}

0 comments on commit eadb00d

Please sign in to comment.