Fizzbuzz Program In C

Well, this is really not a lot of fizzbuzz question, since this is question C.

I wrote simple code in C to print fizzbuzz as required.

#include <stdio.h> int main(void) { int n = 30; int i; for (i = 1; i<=n; i++) printf("%s\n", (i % 15) == 0 ? "fizzbuzz" : (i % 5) == 0 ? "buzz" : (i % 3) == 0 ? "fizz" : i); } 

Now the last else statement obviously does not work, since printf takes a string, while 'i' is an int. My question is, is there any cast that I can apply to convert “i” to string?

EDIT: I have to mention that I really ask if this fizzbuzz test can be done with a single print statement. There is no particular reason why I want this to be the only print statement other than curiosity about whether this can be done.

EDIT2: The answer to the question and here is my implementation:

 #include <stdio.h> int main(void) { int i, n=30; for (i = 1; i<=n; i++) printf((!(i%3) || !(i%5)) ? "%s\n" : "%d\n", !(i % 15) ? "fizzbuzz" : !(i % 5) ? "buzz" : !(i % 3) ? "fizz" : i); } 

http://codepad.org/DN7yBW99

+2
source share
4 answers

You cornered yourself as you tried to put all the logic inside a printf call. It is better to write it “slow” first, and then look for ways to optimize it after.

 for (i = 1; i <= n; i++) { if (i % 15 == 0) printf("fizzbuzz\n"); else if (i % 5 == 0) printf("buzz\n"); else if (i % 3 == 0) printf("fizz\n"); else printf("%d\n", i); } 

Addendum: Do this with only one printf ...

 /* The following code is NOT recommended... */ int isFizz = (i % 3 == 0 ) ? 1 : 0; int isBuzz = (i % 5 == 0 ) ? 1 : 0; int isFizzBuzz = (isFizz && isBuzz) ? 1 : 0; printf( (isFizz || isBuzz) ? "%s\n" : "%d\n", ( isFizzBuzz ? "fizzbuzz" : isFizz ? "fizz" : isBuzz ? "buzz" : i ) ); 

http://codepad.org/LMr5WdIm

+5
source

You can simplify args for printf by passing string literals as a format string, rather than using %s to print them. This exploits the fact that you know that there are no % characters in "fizz" or "buzz" . It is guaranteed that you can not use unused arguments for printing .

To make the source readable, pull the logic to select a format string from printf() and save it in a local variable.

 #include <stdio.h> int main(void) { int n=30; for (int i = 1; i<=n; i++) { int fizzy = (i % 3 == 0); int buzzy = (i % 5 == 0); const char *fmt = (fizzy && buzzy) ? "fizzbuzz\n" : fizzy ? "fizz\n" : buzzy ? "buzz\n" : "%d\n"; printf(fmt, i); } } 

It can be a good style, and perhaps make asm better, conditionally determine the arguments that you are going to pass, and then make one call, instead of writing the same function call with different arguments. This can be useful if most of the arguments you are going to pass are the same in different branches, which is not the case here.

It's usually best to just use puts (implicit \n added) or fputs to print constant lines that don't need formatting. Compilers know this, and even optimize printf("Hello World!\n"); or printf("%s\n", "Hello World!") before puts("Hello World!"); in trivial cases. This trick means that even fixed-line prints still cause the more expensive printf . (In addition to readability, this is one of the specific reasons why this is not a great choice in this particular case.)

gcc and clang compile this into asm, which works like the source does : printf always gets two arguments (in rdi and rsi ), but sometimes the format string doesn't use the 2nd argument. Change -O3 to -Os so that gcc uses a div instead of the multiplicative inverse.

They do not unroll the loop to fit the pattern modulo, or force - reduce the modulus to downward counters or something smart like you would do in handwritten asm FizzBuzz .

For an insidious way to write compact FizzBuzz, see CodeGolf.SE , where the format string %s%s%.d conditionally receives empty strings. %.d sets the precision to 0. Non-zero integers print normally, but 0 prints as an empty string.

+1
source

EDIT Sorry ... I just realized that this thread was for solution C

Recursive implementation:

 vector<string> FizzBuzz::execute(int n) { if(n == 0) return vector<string>(); auto push = n % 15 == 0 ? "fizzbuzz" : n % 3 == 0 ? "fizz" : n % 5 == 0 ? "buzz" : to_string(n); auto execution = execute(n-1); execution.push_back(push); return execution; } 
0
source

how could you use itoa(); . Sample code below.

 #include <stdio.h> #include <stdlib.h> int main(){ int num=23; char snum[3]; itoa(num,snum,10); printf("%s",snum); } 

itoa is used to convert int to string , the parameters are as follows.

 itoa(int num, char string [], int base); 

The first parameter is the number you are converting. The second parameter is an array of strings / char into which you want to save the converted strong. Finally, the third parameter is the base in which the number to be converted is located.

-1
source

Source: https://habr.com/ru/post/1436201/


All Articles