Fizz Buzz Implementation | Set 2 | C ++ program for the above approach ; Function to generate FizzBuzz sequence ; Stores count of multiples of 3 and 5 respectively ; Iterate from 1 to N ; Increment count3 by 1 ; Increment count5 by 1 ; Initialize a boolean variable to check if none of the condition matches ; Check if the value of count3 is equal to 3 ; Reset count3 to 0 , and set flag as True ; Check if the value of count5 is equal to 5 ; Reset count5 to 0 , and set flag as True ; If none of the condition matches ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void fizzBuzz ( int N ) { int count3 = 0 ; int count5 = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { count3 ++ ; count5 ++ ; bool flag = false ; if ( count3 == 3 ) { cout << " Fizz " ; count3 = 0 ; flag = true ; } if ( count5 == 5 ) { cout << " Buzz " ; count5 = 0 ; flag = true ; } if ( ! flag ) { cout << i ; } cout << " ▁ " ; } } int main ( ) { int N = 15 ; fizzBuzz ( N ) ; return 0 ; }