FizzBuzz Test

I do not see what is this buzz about this FizzBuzz Test. It is very simple to code. Fizzbuzz test is as follows
Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz“. For numbers which are multiples of both three and five print “FizzBuzz“.

Here are my solutions

#include <iostream>

int main ( int argc, char **argv) {
	for ( int i = 0; i <= 100; i++) {
		if (  ( i % 3 == 0 ) && (i % 5 == 0 )) {
			std::cout << "FizzBuzz" << std::endl;
		}
		else if ( i % 3 == 0 ) {
			std::cout << "Fizz" << std::endl;
		}
		else if ( i % 5  == 0 ) {
			std::cout << "Buzz" << std::endl;
		} else {
			std::cout << i << std::endl;
		}
	}
	return 0;
}

First if condition can be changed to one

#include <iostream>

int main ( int argc, char **argv) {
	for ( int i = 0; i <= 100; i++) {
		if ( i % 15 == 0 ) {
			std::cout << "FizzBuzz" << std::endl;
		}
		else if ( i % 3 == 0 ) {
			std::cout << "Fizz" << std::endl;
		}
		else if ( i % 5  == 0 ) {
			std::cout << "Buzz" << std::endl;
		} else {
			std::cout << i << std::endl;
		}
	}
	return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s