public
Created

FizzBuzz with recursion and no multiplication or division

  • Download Gist
fizzbuzz-recursive-bit.c
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
 
#include <stdio.h>
#include <stdbool.h>
 
int addDigitsHex(int num);
void recursiveFizzBuzz(int start, int end);
 
int main (int argc, char const *argv[])
{
recursiveFizzBuzz(1, 100);
return 0;
}
 
int addDigitsHex(int num){
// Add up each hex digit, resulting in a single hex digit sum
// Ex: 111 => 3
// Ex: FC3 => 1E => F
int workingNum = num;
int accum = 0;
while(workingNum > 0){
int temp = workingNum & 0xF;
accum += temp;
workingNum >>= 4;
}
if(accum > 0xF){
return addDigitsHex(accum);
}else{
return accum;
}
}
 
void recursiveFizzBuzz(int start, int limit){
if(start <= limit){
switch(addDigitsHex(start)){
//if divisible by 5, If you add up all of the hex digits, it will equal 5, A, or F
case 0x5:
case 0xA:
printf("Buzz\n");
break;
//if divisible by 3, If you add up all of the hex digits, it will equal 3, 6, 9, C or F
case 0x3:
case 0x6:
case 0x9:
printf("Fizz\n");
break;
// F is the only intersection between the possibilities of divisible by 3 and 5
case 0xF:
printf("FizzBuzz\n");
break;
default:
printf("%d\n", start);
break;
}
recursiveFizzBuzz(++start, limit);
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.