{"id":3442,"date":"2019-01-08T00:01:46","date_gmt":"2019-01-08T08:01:46","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3442"},"modified":"2019-01-05T12:18:58","modified_gmt":"2019-01-05T20:18:58","slug":"fizz-buzz-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3442","title":{"rendered":"Fizz Buzz &#8211; Solution"},"content":{"rendered":"<p>The nifty thing about <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3436&#038;preview=true\">this month&#8217;s Exercise<\/a> is that so many solutions are available. In fact, I found the challenge engaging because I kept trying to think of other ways to solve the puzzle.<br \/>\n<!--more--><br \/>\nTo be successful, the output your solution code generates looks something like this:<\/p>\n<pre><code>1\r\n2\r\nFizz\r\n4\r\nBuzz\r\nFizz\r\n7\r\n8\r\nFizz\r\nBuzz\r\n11\r\nFizz\r\n13\r\n14\r\nFizzBuzz\r\n16\r\n17<\/code><\/pre>\n<p>And on up to 100. The output must be consistent no matter what your solution.<\/p>\n<p>For my first crack, I did a straightforward loop with <em>if<\/em> tests to generate the <code>Fizz<\/code> and <code>Buzz<\/code> output:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int a;\r\n\r\n    for(a=1;a&lt;=100;a++)\r\n    {\r\n        if( a % 3 == 0 )\r\n            printf(\"Fizz\");\r\n        if( a % 5 == 0 )\r\n            printf(\"Buzz\");\r\n        if( a%3 &amp;&amp; a%5 )\r\n            printf(\"%d\",a);\r\n        putchar('\\n');\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The first <em>if<\/em> test outputs <code>Fizz<\/code> when <code>a<\/code> is divisible by 3, the second outputs <code>Buzz<\/code> when <code>a<\/code> is divisible by 5. Both tests pass for values divisible by both 3 and 5, so <code>FizzBuzz<\/code> is output.<\/p>\n<p>The third <em>if<\/em> test is TRUE for all values not divisible by either 3 or 5. And finally, the <em>putchar()<\/em> function spits out a newline.<\/p>\n<p>This solution was good, but I wanted something more cryptic!<\/p>\n<p>After a few gyrations, I concocted another solution:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int a;\r\n    char buf[4];\r\n\r\n    for(a=1;a&lt;=100;a++)\r\n    {\r\n        sprintf(buf,\"%d\",a);\r\n        printf(\"%s\\n\",\r\n                a%3 &amp;&amp; a%5 ? buf :\r\n                a%3 ? \"Buzz\" :\r\n                a%5 ? \"Fizz\" :\r\n                \"FizzBuzz\"\r\n              );\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>sprintf()<\/em> function converts the value of <code>a<\/code> into a string. It does this conversion for all values, though not every string created is output.<\/p>\n<p>In the <em>printf()<\/em> function, a series of nested ternary operators generates the proper string to output. This construction is effectively a complex <em>if-else<\/em> test, though I formatted the code to put each initial test on a line by itself:<\/p>\n<p>The first line is the same as the final <em>if<\/em> test in my first solution: If <code>a<\/code> is either divisible by 3 or 5, output the value of <code>a<\/code>, which is text stored in <code>buf<\/code>. Otherwise:<\/p>\n<p>The second line generates <code>Buzz<\/code> when <code>a<\/code> is not equal to 3.<\/p>\n<p>The third line generates <code>Fizz<\/code> when <code>a<\/code> is not equal to 5.<\/p>\n<p>The final line outputs <code>FizzBuzz<\/code> because the only condition not tested for is <code>a<\/code> divided by 15, or both 3 and 5.<\/p>\n<p>For more solutions, including some that will drive you mad, go to Rosetta Code&#8217;s FizzBuzz page, the C solution: <a href=\"http:\/\/rosettacode.org\/wiki\/FizzBuzz#C\" rel=\"noopener\" target=\"_blank\">http:\/\/rosettacode.org\/wiki\/FizzBuzz#C<\/a><\/p>\n<p>I hope your solution is unique and interesting, and I further hope you crafted more than one as you got into the puzzle. That&#8217;s one of the nifty things about learning to program: This puzzle may not prevent world hunger, but working on such problems definitely increases your C programming kung fu, which may eventually prevent world hunger.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The nifty thing about this month&#8217;s Exercise is that so many solutions are available. In fact, I found the challenge engaging because I kept trying to think of other ways to solve the puzzle.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-3442","post","type-post","status-publish","format-standard","hentry","category-solution"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3442","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3442"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3442\/revisions"}],"predecessor-version":[{"id":3454,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3442\/revisions\/3454"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3442"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3442"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}