FizzBuzz Solution Dumping Ground

#171

I failed my intro to computer science course where they taught me java. It’s been over six months. I dont have java installed any more but here’s what I did in my text editor in 5 minutes.

public class NewClass{
public static void main(String[] args){
int i = 1;
while (i <= 100){
if (i % 15 == 0){
System.out.println(“FizzBuzz”);
}else if (1 % 5 == 0){
System.out.println(“Buzz”);
}else if (1 % 3 == 0){
System.out.println(“Fizz”);
}else{
System.out.println(i + “”);
}
}
}
}

I dont have java so I cant test it but im pretty sure i have it or am close. This is just to prove that that 199/200 thing is serious bullshit.

#172

HI guys.
As a student, I feel that the teachers spend a lot of time giving us long long pages of things to put together rather than spending enough time on getting concrete sets of knowledge together-I’ll work on that this year.
My solution uses java-bugger using C and pointers and all that.
Now, I am intending to code for the following:
-print out numbers from 1 to 100 inclusive
-however, numbers that are divisible by 3, get repalced by Fizz, those with 5 get replaced by Buzz, and those that are divisible by both 3 and 5 (i.e by 15) get replaced by FizzBuzz.
I hope my code does what is required:
public class Main {

   public static void main(String[] args) {
     for (int i=1; i<=100; i++){
      if (i%15==0){
          System.out.println("FizzBuzz");
      }else if(i%3==0){
          System.out.println("Fizz");
      }else if(i%5==0){
          System.out.println("Buzz");
      }else{
          System.out.println(i);
      }
  }

}

}

#173

In 4 lines, with PHP

for ($i = 1; $i <= 100; $i++) {
$fizzbuzz = ($i % 3 == 0 ? “Fizz” : “”).($i % 5 == 0 ? “Buzz” : “”);
echo strlen($fizzbuzz) ? $fizzbuzz : $i;
}

#174

Java? Java.

for (int i = 1; i <= 100; i ++)
{
String Mult3 = i % 3 == 0 ? “Fizz” : “”;
String Mult5 = i % 5 == 0 ? “Buzz” : “”;

System.out.println(i + " " + M3 + M5);
}

I like Java…

#175

for(i=1;i<=100;i++){
console.log((i%5 == 0 && i%3 == 0 && “FizzBuzz”) || (i%5 == 0 && “Buzz”) || (i%3 == 0 && “Fizz”) || i);
}

This is how I’d do it in JS

#176

I don’t think this article can be completely correct more so the comments being thrown around. I’m currently in college as a Computer Science major and I had to learn recursion way back in beginning programming class. It was just the hey this is what it is and how you can do it. Since I’ve taken the more advanced classes now. We have to use it in some of the solutions such as Traversing Tree’s or graphs usually. I also learned Hexadecimal and Binary conversions and the operations on them.

Also I would say that my University not the best at all as far University go. It’s one of the cheapest in the state.
Java -
Console run by -
javac FizzBuzz.java
java FizzBuzz
public class FizzBuzz
{
public FizzBuzz()
{
}
public static void main(String[] args)
{
for(int i = 1; i<= 100; i++)
{
if( (i % 3) == 0 && (i % 5) == 0 )
System.out.println(“FizzBuzz”);
else if(i % 5 == 0)
System.out.println(“Buzz”);
else if(i % 3 == 0)
System.out.println(“Fizz”);
else
System.out.println(i);
}
}
}

#177

In C :
#include <stdio.h>

int main()
{
int i=1;
for(i; i<=100;i++)
{
if(i%3==0)
{printf(“FIZZ”);
}if(i%5==0)
{printf(“BUZZ”);
}if(i%3!=0 && i%5!=0)
{printf("%d\n", i);
}else{printf("\n");
}
}
return 0;
}

#178

perl -le’print$%15?$%5?$%3?$:fizz:buzz:fizzbuzz for 1…100’

#179

C# LinqPad Program:
void Main()
{
IEnumerable nums = Enumerable.Range(1, 100);
foreach( int i in nums)
{
string result = (i % 3 == 0) ? “Fizz” : (i % 5 == 0) ? “Buzz” : i.ToString();
result.Dump();
}
}

#180

in C# i can write it in just 2 lines

for (int i = 1; i <= 100; i++)
Console.WriteLine(i.ToString() + " : " + (((i % 3 == 0) && (i % 5 == 0)) ? “FizzBuzz” : (i % 3 == 0 ? “Fizz” : (i % 5 == 0 ? “Buzz”:i.ToString()))));

#181

My C# version

foreach(var i in Enumerable.Range(1,100).Select(i => (i % 15 == 0) ? “FizzBuzz” : (i % 3) == 0 ? “Fizz” : (i % 5) == 0 ? “Buzz” : i.ToString()))
{
Console.Write(i);
}
Console.WriteLine();

Or LINQ

var s = from i in Enumerable.Range(1, 100)
select new
{
value = (i % 15 == 0) ? “FizzBuzz” : (i % 3) == 0 ? “Fizz” : (i % 5) == 0 ? “Buzz” : i.ToString()
};
foreach(var i in s) System.Console.Write(i.value);
Console.WriteLine();

#182

#Short and Sweet ruby answer :

puts (1…100).map{|i| i%15 == 0 ? “Fizzbuzz” : i%3 == 0 ? “Fizz” : i%5 == 0 ? “buzz” : “#{i}” }

#183

Python version:

for number in range(1, 101):
    result = ""
    if not number % 3:
        result = "Fizz"
    if not number % 5:
        result += "Buzz"
    if not result:
        result = number
    print result
#184

in javascript, fun with lazy ||

for (var n = 1; n <= 100; ++n) {
    console.log ((function() {
        var s, t1 = n % 3 == 0, t2 = n % 5 == 0, t3 = t1 && t2;
        if ((s = "fizzbuzz", t3) || (s = "fizz", t1) || (s = "buzz", t2))
            return [n, " is ", s].join("");
        return n;
    })());
    
}

http://jsfiddle.net/dimitar/F52fn/5/

#185

Sure this is an old thread, but I wanted to put my two cents in. In Java:

	String out = "";
	
	for(int x = 1; x <= 100; x++){
		//reset the variable with each loop
		out = "";			
		//if the number is divisible by 3, show Fizz
		if(x % 3 == 0) out += "Fizz";
		//if the number is divisible by 5, show Buzz
		if(x % 5 == 0) out += "Buzz";
		//If neither case above, then show the number
		if(out == "") out = ""+x;
		System.out.println(out);
#186

This is my solution:

1 Like
#187

A Scala REPL version…

for ( i <- 1 to 100 ) {
  var f = if ( i%3 == 0 ) { "Fizz" } else { "" } 
  var b = if ( i%5 == 0 ) { "Buzz" } else { "" }
  var r = if ( (f+b).size == 0 ) { i } else { f+b }
  println ( r )
}
#188
for (int i = 0; i<100; i++)
{
	if(i%3==0)
		Console.Write("Fizz");
	if(i%5==0)
{
		Console.WriteLine("Buzz");
		continue;
}
	Console.Writeline(i.toString());	
			 
}
#189

// Making it look good in java :wink: took me 4 min because I’m dumb and I’m still in high school.

public class FizzBuzzMultiples extends ConsoleProgram {

public void run() {
	for(double i = 1; i<101; i++) {
		int q = 0;
		double j = i/5;

		if(j%2 == 0) {
			double ok = j/3;
			if(ok%2 == 0) {
				println(i+ "- FizzBuzz");
				q = 1;
			}
		}
		if(q == 0) {
			if(i % 3 == 0) {
				println(i+ "- Fizz");
			}else if(i % 5 == 0) {
				println(i+ "- Buzz");
			} else {
				println(i);
			}
		}

		q = 0;
	}	
}
 }
#190
<?php
for ($i=1; $i <= 100 ; $i++) { 
	if($i % 3 === 0 && $i % 5 !== 0){
		echo 'Fizz<br>';
	} else if($i % 5 === 0 && $i % 3 !== 0){
		echo 'Buzz<br>';
	} else if($i % 5 === 0 && $i % 3 === 0){
		echo 'FizzBuzz<br>';
	} else {
		echo $i.'<br>';
	}
}
?>