Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,194,813 members, 7,956,073 topics. Date: Monday, 23 September 2024 at 12:19 AM |
Nairaland Forum / Science/Technology / Programming / Fizzbuzz Problem Using "Switch" Statement Construct (3944 Views)
Who Can Solve This Problem Using Java / The Switch Statement Is Redundant / Andela Lab Solution(fizzbuzz) (2) (3) (4)
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.
|
Re: Fizzbuzz Problem Using "Switch" Statement Construct by KazukiIto(m): 7:08pm On Nov 12, 2015 |
omoelu1: 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: |
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
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: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:Nice |
Re: Fizzbuzz Problem Using "Switch" Statement Construct by danvery2k6(m): 10:39pm On Nov 13, 2015 |
PHP Version
|
Re: Fizzbuzz Problem Using "Switch" Statement Construct by danvery2k6(m): 10:46pm On Nov 13, 2015 |
Ruby
|
Re: Fizzbuzz Problem Using "Switch" Statement Construct by Jelo4kul(m): 11:03pm On Nov 13, 2015 |
KazukiIto: 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 |
Re: Fizzbuzz Problem Using "Switch" Statement Construct by KazukiIto(m): 10:07am On Nov 16, 2015 |
Jelo4kul: 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 KazukiIto: |
Re: Fizzbuzz Problem Using "Switch" Statement Construct by danvery2k6(m): 11:18am On Nov 17, 2015 |
Raypawer: 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: 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: good for you, never saw it from js angle Metalzoa: |
Re: Fizzbuzz Problem Using "Switch" Statement Construct by danvery2k6(m): 10:20am On Nov 19, 2015 |
Raypawer: 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 , 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#.. danvery2k6: |
Re: Fizzbuzz Problem Using "Switch" Statement Construct by Metalzoa: 10:00am On Nov 25, 2015 |
Raypawer: 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: 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: |
Re: Fizzbuzz Problem Using "Switch" Statement Construct by sunnico(m): 10:01pm On Nov 25, 2015 |
Metalzoa: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: I never said you were dumb... If you are suggesting it, that's fine by me... |
(1) (Reply)
Everybody Is A Javascript Developer On Nigerian Tech Twitter / New To Python, Gurus Please Help Solve These Challenges. Thanks. / HELP! My Exposure To My System Due To Coding And Research Is Affecting My Eye.
(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. 50 |