From 64765ca89fdbd917fe97ffab4cad053095ecbc10 Mon Sep 17 00:00:00 2001 From: Nilesh Halge Date: Sun, 6 Oct 2019 00:48:20 +0530 Subject: [PATCH] Create FizzBuzz.cpp FizzBuzz Solution in cpp using standard template library. --- FizzBuzz.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 FizzBuzz.cpp diff --git a/FizzBuzz.cpp b/FizzBuzz.cpp new file mode 100644 index 0000000..f65015d --- /dev/null +++ b/FizzBuzz.cpp @@ -0,0 +1,50 @@ +// CPP program to print Fizz Buzz +#include +#include +#include + +int main() +{ + + std :: vector range(100); + + // 'iota' to fill the vector in increasing manner + std :: iota(range.begin(), range.end(), 1); + + // initializing dynamic array of string type + std :: vector values; + + // resize the vector(values) as that of vector(range) + values.resize(range.size()); + + + auto fizzbuzz = [](int i) -> std::string + { + // number divisible by 15(will also be + // divisible by both 3 and 5), print 'FizzBuzz' + if ((i%15) == 0) + return "FizzBuzz"; + + // number divisible by 5, print 'Buzz' + if ((i%5) == 0) + return "Buzz"; + + // number divisible by 3, print 'Fizz' + if ((i%3) == 0) + return "Fizz"; + + // to print other numbers + return std::to_string(i); + }; + + // Operation to each of the elements in the + // range [begin(), end()) and stores the + // value returned by each operation in the + // range that begins at values.begin(). + std :: transform(range.begin(), range.end(), + values.begin(), fizzbuzz); + for (auto& str: values) + std::cout << str << std::endl; + + return 0; +}