Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- FizzBuzz for the modern age.
- Plays the age old classic game fizzbuzz between 1 and 100.
- The rules are as follows.
- If a number is a multiple of 3 then print "Fizz".
- If a number is a multiple of 5 then print "Buzz".
- If a number is a multiple of both 3 and 5 then print "FizzBuzz".
- If a number is not a multiple of 3 nor a multiple of 5 print the
- number.
- Each out put should be on a new line.
- This example requires c++11 or higher, due to the use of to_string,
- although it could be simply adapted to work with older standards.
- The example also makes use of in-line if statements, this may or not
- be favoured by everyone, but I like them.
- Copyright (c) 2014 Morgan Hill <morgan@pcwizzltd.com>
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- #include <iostream>
- #include <string>
- using namespace std;
- int main (){
- for (int i = 1; i<=100; ++i){
- string output = "";
- output += i % 3 == 0 ? "Fizz":"";
- output += i % 5 == 0 ? "Buzz":"";
- output += output.length() == 0 ? to_string(i):"";
- cout << output << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement