Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,148,906 members, 7,802,945 topics. Date: Saturday, 20 April 2024 at 04:40 AM

Fizzbuzz Problem Using "Switch" Statement Construct - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Fizzbuzz Problem Using "Switch" Statement Construct (3760 Views)

Who Can Solve This Problem Using Java / The Switch Statement Is Redundant / Andela Lab Solution(fizzbuzz) (2) (3) (4)

(1) (Reply) (Go Down)

Fizzbuzz Problem Using "Switch" Statement Construct by KazukiIto(m): 7:51pm On Nov 11, 2015
Hi guys!
We all know of the FizzBuzz problem:

Write a program (any language) that prints from 1 to 100. For
the multiples of 3, print "Fizz"; for the multiples of 5, print
"Buzz"; for the multiples of both 3 and 5, print "FizzBuzz".


I have already implemented this program using "if, if..else clauses",
but I want to implement this using switch statement(s) instead.
The program I came up with is f***ed up on so many levels.
My program goes:

[center]// all headers and protocols duly observed

for (int i = 1; i <= 100; i++)
{
switch (i)

case 1:
( i % 15 == 0 );
cout << "FizzBuzz";
break;

case 2:
( i % 3 == 0 );
cout << "Fizz";
break;

case 3:
( i % 5 == 0 );
cout << "Buzz"
break;

default:
cout << i << ", ";
break;
}
[/center]
The program will now output FizzBuzz, 1, 2, 3, ... , 100. This is
not even qualified to be called "wrong". So any ideas on how I
solve this problem using switch statement?

PS: I used C++ for this, but any language that has switch construct
is highly welcomed. I just want to get the correct logic for solving
this.

Thanks for your time...
Re: Fizzbuzz Problem Using "Switch" Statement Construct by omoelu1(m): 10:43pm On Nov 11, 2015
Were you specifically told to solve it using switch construct?
if yes, I wonder what the solution will be, myself.

I have been trying to work out a possible solution though,
but the problem is - to test if a number is even or odd, a boolean value will be returned, but a switch statement case can only check for integer values.

So, I am not quite sure that is possible.

Would love to see someone do this.
Re: Fizzbuzz Problem Using "Switch" Statement Construct by DonSegmond(m): 2:59am On Nov 12, 2015
Don't need a switch statement.


for ($i = 1; $i <= 100; $i++) {
$str = (($i % 3 == 0) ? "Fizz" : "" ) . (($i % 5 == 0) ? "Buzz" : "" );
echo ($str == "" ) ? "$i\n" : "$str\n";
}
Re: Fizzbuzz Problem Using "Switch" Statement Construct by KazukiIto(m): 7:08pm On Nov 12, 2015
omoelu1:
Were you specifically told to solve it using switch construct?
if yes, I wonder what the solution will be, myself.

I have been trying to work out a possible solution though,
but the problem is - to test if a number is even or odd, a boolean value will be returned, but a switch statement case can only check for integer values.

So, I am not quite sure that is possible.

Would love to see someone do this.

I was just curious to see if switch can perform the trick. Will certainly like to see someone with a solution though!
Re: Fizzbuzz Problem Using "Switch" Statement Construct by KazukiIto(m): 7:10pm On Nov 12, 2015
DonSegmond:
Don't need a switch statement.


for ($i = 1; $i <= 100; $i++) {
$str = (($i % 3 == 0) ? "Fizz" : "" ) . (($i % 5 == 0) ? "Buzz" : "" );
echo ($str == "" ) ? "$i\n" : "$str\n";
}

smiley
Re: Fizzbuzz Problem Using "Switch" Statement Construct by makavele: 12:30am On Nov 13, 2015
You can't use "case" on an object . . . I might be wrong . . .
Re: Fizzbuzz Problem Using "Switch" Statement Construct by Metalzoa: 5:51am On Nov 13, 2015
Interesting question.

Solved using JavaScript ... my favorite language smiley


function do_fizzbuzz( input ){

if( !input ) return false;

var output;

switch( true ){

case (input % 15 === 0):
output = 'FizzBuzz';
break;

case (input % 3 === 0):
output = 'Fizz';
break;

case (input % 5 === 0):
output = 'Buzz';
break;

default:
output = input;
break;
}

return output;
}



Link to solution in action: [url=http://jsbin.com/suvugeveta/edit?js%2Coutput]JSBIN[/url]
Re: Fizzbuzz Problem Using "Switch" Statement Construct by omoelu1(m): 1:06pm On Nov 13, 2015
Metalzoa:
Interesting question.

Solved using JavaScript ... my favorite language smiley


function do_fizzbuzz( input ){

if( !input ) return false;

var output;

switch( true ){

case (input % 15 === 0):
output = 'FizzBuzz';
break;

case (input % 3 === 0):
output = 'Fizz';
break;

case (input % 5 === 0):
output = 'Buzz';
break;

default:
output = input;
break;
}

return output;
}



Link to solution in action: [url=http://jsbin.com/suvugeveta/edit?js%2Coutput]JSBIN[/url]
nice one. but this won't work in java
Re: Fizzbuzz Problem Using "Switch" Statement Construct by danvery2k6(m): 9:49pm On Nov 13, 2015
Metalzoa:
Interesting question.

Solved using JavaScript ... my favorite language smiley


function do_fizzbuzz( input ){

if( !input ) return false;

var output;

switch( true ){

case (input % 15 === 0):
output = 'FizzBuzz';
break;

case (input % 3 === 0):
output = 'Fizz';
break;

case (input % 5 === 0):
output = 'Buzz';
break;

default:
output = input;
break;
}

return output;
}



Link to solution in action: [url=http://jsbin.com/suvugeveta/edit?js%2Coutput]JSBIN[/url]
Nice
Re: Fizzbuzz Problem Using "Switch" Statement Construct by danvery2k6(m): 10:39pm On Nov 13, 2015
PHP Version


function fizzBuzz($input)
{

switch ($input) {
case $input % 15 === 0:
print "fizzbuzz<br/>";
break;

case $input % 5 === 0:
print 'buzz <br/>';
break;

case $input % 3 === 0:
print 'fizz<br/>';
break;

default:
print $input . "<br/>";
break;
}
}

function doFizzBuzz()
{
foreach (range(1, 100) as $index) {
fizzBuzz($index);
}
}

doFizzBuzz();
Re: Fizzbuzz Problem Using "Switch" Statement Construct by danvery2k6(m): 10:46pm On Nov 13, 2015
Ruby


def fizzbuzz(num)
case
when num % 15 == 0 then "FizzBuzz"
when num % 3 == 0 then "Fizz"
when num % 5 == 0 then "Buzz"
else num
end
end

def fizz_buzz_to(limit)
1.upto(limit).each do |num|
puts fizzbuzz(num)
end
end

fizz_buzz_to(100);

Re: Fizzbuzz Problem Using "Switch" Statement Construct by Jelo4kul(m): 11:03pm On Nov 13, 2015
KazukiIto:
Hi guys!
We all know of the FizzBuzz problem:
[b]
Write a program (any language) that prints from 1 to 100. For
the multiples of 3, print "Fizz"; for the multiples of 5, print
"Buzz"; for the multiples of both 3 and 5

Do you want a version in Java?
Re: Fizzbuzz Problem Using "Switch" Statement Construct by makavele: 10:18am On Nov 14, 2015
This is a more dynamic method that accepts user input via variables:


<?php

$low = 1;
$high = 50;

class FizBuzz {

public function PrintFizzBuzz($n) {

global $low;
global $high;

switch ($n) {
case (($n % 3 == 0) && ($n % 5 != 0)):
echo $n . ' is a multiple of 3 ------fizz' . '<br>';
break;

case (($n % 5 == 0) && ($n % 3 != 0)):
echo $n. ' is a multiple of 5---------buzz' . '<br>';
break;

case (($n % 3 == 0) and ($n % 5 ==0)):
echo $n . ' is a multiple of 15-----------fizzbuzz' . '<br>';
break;

default:
echo $n . '<br>';
break;
}

}

public function SupplyValues($low, $high) {

foreach (range($low, $high) as $x) {

$this->PrintFizzBuzz($x);

}

}

}

$nairaland = new FizBuzz();
$nairaland->SupplyValues($low, $high);

?>
Re: Fizzbuzz Problem Using "Switch" Statement Construct by KazukiIto(m): 10:07am On Nov 16, 2015
Jelo4kul:


Do you want a version in Java?

sure
Re: Fizzbuzz Problem Using "Switch" Statement Construct by Raypawer(m): 11:25am On Nov 16, 2015
Am not sure any of these codes above me would work, because basically Switches cannot have evaluated statements in the case statement. They must be statically evaluated shocked shocked shocked


KazukiIto:
Hi guys!
We all know of the FizzBuzz problem:

Write a program (any language) that prints from 1 to 100. For
the multiples of 3, print "Fizz"; for the multiples of 5, print
"Buzz"; for the multiples of both 3 and 5, print "FizzBuzz".

I have already implemented this program using "if, if..else clauses",
but I want to implement this using switch statement(s) instead.
The program I came up with is f***ed up on so many levels.
My program goes:
[center]// all headers and protocols duly observed
for (int i = 1; i <= 100; i++)
{
switch (i)

case 1:
( i % 15 == 0 );
cout << "FizzBuzz";
break;

case 2:
( i % 3 == 0 );
cout << "Fizz";
break;

case 3:
( i % 5 == 0 );
cout << "Buzz"
break;

default:
cout << i << ", ";
break;
}
[/center]
The program will now output FizzBuzz, 1, 2, 3, ... , 100. This is
not even qualified to be called "wrong". So any ideas on how I
solve this problem using switch statement?

PS: I used C++ for this, but any language that has switch construct
is highly welcomed. I just want to get the correct logic for solving
this.

Thanks for your time...
Re: Fizzbuzz Problem Using "Switch" Statement Construct by danvery2k6(m): 11:18am On Nov 17, 2015
Raypawer:
Am not sure any of these codes above me would work, because basically Switches cannot have evaluated statements in the case statement. They must be statically evaluated shocked shocked shocked

Try it out before you comment. Stop thinking, Start acting.
Re: Fizzbuzz Problem Using "Switch" Statement Construct by Metalzoa: 4:15pm On Nov 17, 2015
Raypawer:
Am not sure any of these codes above me would work, because basically Switches cannot have evaluated statements in the case statement. They must be statically evaluated shocked shocked shocked

But mine has a link to it working. Did you try the link?

[url=http://jsbin.com/suvugeveta/edit?js%2Coutput]JAVASCRIPT SOLUTION[/url]
Re: Fizzbuzz Problem Using "Switch" Statement Construct by Raypawer(m): 7:21pm On Nov 17, 2015
Hey! hold it there, tried it with c# it never worked, that made me to read again on switch statement, that gave me the conclusion,

but for the later that worked on javascript,yes i should, js is interpreted not compiled, its actually a scripting language, it may also work on php and other scripting lang.

danvery2k6:

Try it out before you comment. Stop thinking, Start acting.


good for you, never saw it from js angle

Metalzoa:


But mine has a link to it working. Did you try the link?

[url=http://jsbin.com/suvugeveta/edit?js%2Coutput]JAVASCRIPT SOLUTION[/url]
Re: Fizzbuzz Problem Using "Switch" Statement Construct by danvery2k6(m): 10:20am On Nov 19, 2015
Raypawer:
Hey! hold it there, tried it with c# it never worked


So, you can't paste the same code into another language and expect it to work. You have to tweak things to get it working.
Re: Fizzbuzz Problem Using "Switch" Statement Construct by Raypawer(m): 10:22am On Nov 24, 2015
what is this one talking about angry angry , i don't do copy and past of code, i tried the logic he explained using switch statement not copying and past. Am not that dumb if you are, the op used c++, i used the same logic with c#.. undecided undecided

danvery2k6:


So, you can't paste the same code into another language and expect it to work. You have to tweak things to get it working.
Re: Fizzbuzz Problem Using "Switch" Statement Construct by Metalzoa: 10:00am On Nov 25, 2015
Raypawer:
what is this one talking about angry angry , i don't do copy and past of code, i tried the logic he explained using switch statement not copying and past. Am not that dumb if you are, the op used c++, i used the same logic with c#.. undecided undecided


But even that doesn't work because low level behavior in languages differ, so you can't copy the same logic without accounting for those differences.

For example in php, the case is it's own separate comparison and doesn't check back with what was inputted into the switch statement.

I didn't know this, so it didn't look like the php version would work to me. I tried it, it worked and I learned something new about php.

Just because a switch works one way in a language doesn't mean it works exactly the same way in another.
Re: Fizzbuzz Problem Using "Switch" Statement Construct by sunnico(m): 1:41pm On Nov 25, 2015
The way I see this program you have only 3 cases,
1. when modulus 3 gives 0
2. when modulus 5 gives 0
3. and when modulus 15 gives 0

so this cases are what u should switch

here is what i did

#include <iostream>
using namespace std;
int main(){
int cases=0;
for (int i = 1; i <= 100; i++)
{
//setting my cases
if(i%15==0)
{cases=3; }
else if(i%5==0)
{cases=2;}
else if(i%3==0)
{cases=1 ; }
//for those not meeting ur selection//
else{cases=0;}

switch (cases)
{
case 0:
cout <<i<<endl;
break;

case 1:
cout <<i<< "Fizz"<<endl;
break;

case 2:
cout <<i<< "Buzz" <<endl ;
break;

case 3:
cout <<i<< "FizzBuzz"<<endl;
break;

default:
cout << "how did i get here" << endl;
break;
}
}




return 0;
}
Re: Fizzbuzz Problem Using "Switch" Statement Construct by Metalzoa: 5:47pm On Nov 25, 2015
sunnico:
The way I see this program you have only 3 cases,
1. when modulus 3 gives 0
2. when modulus 5 gives 0
3. and when modulus 15 gives 0

so this cases are what u should switch

here is what i did

#include <iostream>
using namespace std;
int main(){
int cases=0;
for (int i = 1; i <= 100; i++)
{
//setting my cases
if(i%15==0)
{cases=3; }
else if(i%5==0)
{cases=2;}
else if(i%3==0)
{cases=1 ; }
//for those not meeting ur selection//
else{cases=0;}

switch (cases)
{
case 0:
cout <<i<<endl;
break;

case 1:
cout <<i<< "Fizz"<<endl;
break;

case 2:
cout <<i<< "Buzz" <<endl ;
break;

case 3:
cout <<i<< "FizzBuzz"<<endl;
break;

default:
cout << "how did i get here" << endl;
break;
}
}




return 0;
}

Isn't that cheating?

You're doing the work with an if / else struct then outputting the result via a switch.
Re: Fizzbuzz Problem Using "Switch" Statement Construct by Raypawer(m): 6:52pm On Nov 25, 2015
help yourself by removing the word 'copy'. i tried the Logic witth c#. I just realized that the only compiled language for now that does that with switch is Golang..

Metalzoa:


But even that doesn't work because low level behavior in languages differ, so you can't copy the same logic without accounting for those differences.

For example in php, the case is it's own separate comparison and doesn't check back with what was inputted into the switch statement.

I didn't know this, so it didn't look like the php version would work to me. I tried it, it worked and I learned something new about php.

Just because a switch works one way in a language doesn't mean it works exactly the same way in another.
Re: Fizzbuzz Problem Using "Switch" Statement Construct by sunnico(m): 10:01pm On Nov 25, 2015
Metalzoa:


Isn't that cheating?

You're doing the work with an if / else struct then outputting the result via a switch.
Cheating!!! no its tweaking
well looking at d use of switch, normally ur program case would involve u including all possible consequences, meaning u wud be having more than 3 cases to define an output for. That way u wud be so righteous to write many case statement for ur program.
Thé problem is thé case statement requires à constant integer, so for ur program(in c++) its either u include all cases i.e when i=3,i=5,i=6,i=9,i=10, i=15,....which i tink is dumb
or u stick to ur if.. else if..
Re: Fizzbuzz Problem Using "Switch" Statement Construct by danvery2k6(m): 9:22am On Nov 26, 2015
Raypawer:
what is this one talking about angry angry , i don't do copy and past of code, i tried the logic he explained using switch statement not copying and past. Am not that dumb if you are, the op used c++, i used the same logic with c#.. undecided undecided


I never said you were dumb... If you are suggesting it, that's fine by me...

(1) (Reply)

Hi Guys! Created A Signup Form Using HTML5/CSS3. Source Code Available. / Facebook Database Schema (take A Look *parental Advisory*) / Expert Systems With Visual Basic

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 52
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.