Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”. Print a new line after each string or number.
Here is C and C++ solutions
Fizz Buzz C Solution :
#include <stdio.h> #define gc getchar_unlocked #define pc putchar_unlocked inline int scan(){register int n=0,c=gc();while(c<'0'||c>'9')c=gc();while(c<='9'&&c>='0')n=(n<<1)+(n<<3)+c-'0',c=gc();return n;} int printFB(int n){ int i,j; for(i=1;i<=n;i++){ if(i%3==0 && i%5==0)printf("FizzBuzz\n"); else if(i%3==0)printf("Fizz\n"); else if(i%5==0)printf("Buzz\n"); else printf("%d\n",i); } } int main(void) { int t,n; t=scan(); while(t--){ n=scan(); printFB(n); } return 0; }
Fizz Buzz C++ Solution
#include <iostream> using namespace std; int main() { int T,num, i=1;; cin >> T; while(T > 0) { cin>>num; cout<<"number="<<num<<"T="<<T<<endl; i=1; while(i<=num){ if( i%15 == 0) { cout << "FizzBuzz" << endl; } else if(i%5 == 0 ){ cout << "Buzz" << endl; } else if(i%3 == 0) { cout << "Fizz" << endl; } else { cout<<i<< endl; } i++; } T--; } return 0; }
[…] 1 ) Print Fizz Buzz […]