C programming. FizzBuzz Program

I had a survey and I wrote this code:

Print Fizz if it is divisible by 3, and it prints Buzz if it divides by 5. It prints FizzBuss if it is divisible by both. Otherwise, it will print numbers from 1 to 100.

But after I got home, I wondered if I could write it with less code. However, I could not exit with a short code. Can I do this with shorter code? Thanks.

This is what I wrote, and I think it works well. But can I do this with less code.

#include <stdio.h> int main(void) { int i; for(i=1; i<=100; i++) { if(((i%3)||(i%5))== 0) printf("number= %d FizzBuzz\n", i); else if((i%3)==0) printf("number= %d Fizz\n", i); else if((i%5)==0) printf("number= %d Buzz\n", i); else printf("number= %d\n",i); } return 0; } 
+6
source share
11 answers

You can also do:

 #include <stdio.h> int main(void) { int i; for(i=1; i<=100; ++i) { if (i % 3 == 0) printf("Fizz"); if (i % 5 == 0) printf("Buzz"); if ((i % 3 != 0) && (i % 5 != 0)) printf("number=%d", i); printf("\n"); } return 0; } 

A few lines are shorter and much easier to read.

+11
source

If the number is divided by 3 and 5, then it is divided by 15, therefore:

 for each number 1 to 100: if number % 15 == 0: print number, "fizzbuzz" else if number % 5 == 0: print number, "buzz" else if number % 3 == 0: print number, "fizz" else: print number 

Other than that, you probably won’t get it much shorter, at least in a regular language like C (and I assume you don’t want regular code style and golf style modifications to make your code unreadable).

You could also get all this in two lines if you packed the entire main function on one big line, but I hope that you won't be after this cheat either.

You can get it faster (although you should check all the performance requirements for yourself) with something like:

 static const char *xyzzy[] = { "", "", "fizz", "", "buzz", "fizz", "", "", "fizz", "buzz", "", "fizz", "", "buzz", "fizzbuzz", // Duplicate those last three lines to have seven copies (7x15=105). }; for (int i = 1; i <= 100; i++) printf ("%d %s\n", i, xyzzy[i-1]); 

As an aside, this array of char pointers is likely to be cheaper than you think, thanks to constant concatenation - in other words, it is likely that there will be only one of each line of C.

As I said, do I need to test faster. In addition, only the shortest code is called in your original specifications, so it may not be relevant.

+7
source

I'm not sure when you will start calling it unreadable, but here it is.

 #include <stdio.h> int main(void) { int i = 1; for (; i<=100; ++i) { printf("number= %d %s%s\n", i, i%3?"":"Fizz", i%5?"":"Buzz"); } return 0; } 
+7
source

I would say that modulo is expensive, while comparisons are cheap, so only do modulo once. This will give something like this.

 int i; for( i = 0; i!=100; ++i ) { bool bModThree = !(i % 3); bool bModFive = !(i % 5); if( bModThree || bModFive ) { if( bModThree ) { printf( "Fizz" ); } if( bModFive ) { printf( "Buzz" ); } } else { printf( "%d", i ); } printf( "\n" ); } 
+2
source

I would write something like this

  main(){ if (i % 3 == 0){ cout<<"Fizz"; } if (i % 5 == 0){ cout<<"Buzz"; } // So if both are true, it will print "FizzBuzz" and augment the two strings } 
+1
source
 #include <stdio.h> char const * template[] = { "%i", "Buzz", "Fizz", "FizzBuzz" }; const int __donotuseme3[] = { 2, 0, 0 }; const int __donotuseme5[] = { 1, 0, 0, 0, 0 }; #define TEMPLATE(x) (template[__donotuseme3[(x) % 3] | __donotuseme5[(x) % 5]]) int main(void) { int i; for (i = 1; i <= 100; i++) { printf(TEMPLATE(i), i); putchar('\n'); } return 0; } 
0
source

This avoids code repetition, but requires a temporary variable char t

 void FizzBuzz( ) { char t = 0; for (unsigned char i = 1; i <= 100; ++i, t = 2) { (i % 3) ? --t : printf("Fizz"); (i % 5) ? --t : printf("Buzz"); if (!t) printf("%d", i); printf("\n"); } } 
0
source
 void main() { int i = 0; char h[4]; while (++i <= 100) { sprintf(h, "%d", i); printf("%s%s%s\n", i%3 ? "" : "fizz", i%5 ? "" : "buzz", (i%3 && i%5) ? h: ""); } } 
0
source

You can do this using the line:

 String s=""; if(num%3==0) s+="fizz"; if(num%5==0) s+="buzz"; if(s.length()==0) s+=num+""; 
0
source

I would go with a helper function :-)

 #include <stdio.h> int fbindex(int n) { int i = 0; if (n % 3 == 0) i += 1; if (n % 5 == 0) i += 2; return i; } int main(void) { const char *fb[] = {"%d\n", "Fizz\n", "Buzz\n", "FizzBuzz\n"}; for (int i = 1; i <= 100; i++) printf(fb[fbindex(i)], i); } 
0
source

Mr. Lister's Obfuscated Response Form

main(int i){while(i++<100){printf("number= %d %s%s",i,i%3?"":"Fizz",i%5?"":"Buzz");}}

-3
source

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


All Articles