Skip to content

Instantly share code, notes, and snippets.

@remram44
Last active August 29, 2017 18:33
Show Gist options
  • Save remram44/d68927104b0e8a6f5ac8a417b25c018d to your computer and use it in GitHub Desktop.
Save remram44/d68927104b0e8a6f5ac8a417b25c018d to your computer and use it in GitHub Desktop.
FizzBuzz with std::nexttoward()
#include <cmath>
#include <iostream>
int main() {
// Bounds: from 1 to 100 (here we are assuming inclusive)
float current = 1.0, to = 100.0;
// The next integer to test
int next = 2;
// Loop on all floating-point numbers up to 'to'
for(; current <= to; current = std::nexttoward(current, to)) {
// Whether we have found a "special" number
// (don't print it, ONLY the text)
bool special = false;
// If we hit an integer
if(current == next) {
// Multiple of 3
if(next % 3 == 0) {
std::cout << "Fizz";
special = true;
}
// Multiple of 5
if(next % 5 == 0) {
std::cout << "Buzz";
special = true;
}
// Update next integer to test
next++;
}
// If not a "special" number, print the number itself
if(!special) {
std::cout << current;
}
// Print a newline between numbers
std::cout << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment