5 Replies - 5994 Views - Last Post: 27 August 2012 - 11:53 AM

#1 RUAg4mer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 30-July 11

FizzBuzz without Mod

Posted 27 August 2012 - 11:30 AM

I recently looked up how to do the FizzBuzz output without modular division. This code works, but I can't seem to understand how it works. Can someone tell me whats going on here?

#include <stdio.h>
void fizzbuzz(int num_fizz, int num_buzz, int num_fizzbuzz, int counter)
{    
	if(num_fizzbuzz == 15) 
	{
		printf("fizzbuzz "); 
		num_fizz=0; num_buzz=0; num_fizzbuzz=0;
	}    
	else if(num_fizz == 3) 
	{
		printf("fizz "); num_fizz=0;
	}    
	else if(num_buzz == 5) 
	{
		printf("buzz "); num_buzz=0;
	}    
	else printf("%d ", counter);    
	if (counter < 100) 
		fizzbuzz(++num_fizz, ++num_buzz, ++num_fizzbuzz, ++counter);
	}
int main ()
{    
	fizzbuzz(1,1,1,1);
	return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: FizzBuzz without Mod

#2 RUAg4mer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 30-July 11

Re: FizzBuzz without Mod

Posted 27 August 2012 - 11:38 AM

I think I see it now. Pretty much resetting to zero everytime and then incrementing the counter. So it counts to 3 and fizzes, resets back to zero and fizzes in another 3 increments. so on and so on
Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7872
  • View blog
  • Posts: 13,350
  • Joined: 19-March 11

Re: FizzBuzz without Mod

Posted 27 August 2012 - 11:39 AM

Step through it. It's pretty obvious when you run it in your head, or do it with a piece of paper.

What happens on the first round through? The second? The third?

EDIT: Yep, you got it. Cute, huh?

This post has been edited by jon.kiparsky: 27 August 2012 - 11:39 AM

Was This Post Helpful? 0
  • +
  • -

#4 RUAg4mer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 30-July 11

Re: FizzBuzz without Mod

Posted 27 August 2012 - 11:39 AM

I'd delete this but I don't know how. Feel free too Mr. Mod
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6075
  • View blog
  • Posts: 23,540
  • Joined: 23-August 08

Re: FizzBuzz without Mod

Posted 27 August 2012 - 11:46 AM

We don't delete topics here.
Was This Post Helpful? 0
  • +
  • -

#6 RUAg4mer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 30-July 11

Re: FizzBuzz without Mod

Posted 27 August 2012 - 11:53 AM

View Postjon.kiparsky, on 27 August 2012 - 11:39 AM, said:

EDIT: Yep, you got it. Cute, huh?


Yea it's pretty cool.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1