Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

fizzbuzz c++

#include <iostream>
using namespace std;

void fizzBuzz(int n)
{
	for (int i = 1; i <= n; i++)
	{
		if (i % 3 != 0 && i % 5 != 0) //check if the number is neither a multiple of 5 nor 3
		{
			cout << i << endl;
		}
		else if (i % 3 == 0) //check if the number is a multiple of 3
		{
			if (i % 5 == 0) //check if the number is also a multiple of 5
			{
				cout << "FizzBuzz" << endl;
			}
			else
			{
				cout << "Fizz" << endl;
			}
		}
		else
		{
			cout << "Buzz" << endl; //if the number didn't satisfy the first if statement or the else if statement then it is a multiple of 5
		}
	}
}

int main()
{
	fizzBuzz(15);

	return 0;
}
Source by github.com #
 
PREVIOUS NEXT
Tagged: #fizzbuzz
ADD COMMENT
Topic
Name
4+7 =