My first dev blog. I've been working on my personal blog site, so I figured I'd test the waters and do a quick copy and paste of my most recent blog post.
I'm new to this 😬
I’ve been wanting to blog, but I’ve been extremely busy with school and other projects.
SOLUTION TO MY PROBLEM
Blog about potential interview questions with my take on the solution!
THIS IS MY TAKE ON FIZZ BUZZ (IN C#)
🙋♂️ Public Service Announcement 👇
I used a ternary conditional operator (because I need practice using them), and I used the foreach loop (I’m just now getting familiar with).
Here we go…
Fizz buzz = Print numbers 1 through 100. When a number is divisible by 3, print fizz. If a number is divisible by 5, print buzz. And if a number is divisible by both 3 and 5, print fizz buzz.
using System;
namespace foreachFizzbuzz
{
class Program
{
static void Main(string[] args)
{
int[] fizzBuzzArr = new int [100]; // array set to 100 different integers
int i = 0; // our counter to fill
foreach(int value in fizzBuzzArr) // begin loop
{
i++;
Console.Write((i % 3) == 0 && (i % 5) == 0 ? ($"{i} (fizz buzz)\n\n") // initial if
: (i % 3) == 0 ? ($"{i} (fizz), ") // else if
: (i % 5) == 0 ? ($"{i} (buzz)\n\n") // else if
: ($"{i}, ")); // else
} // end loop
Console.ReadLine();
}
}
}
REVISED
using System;
namespace foreachFizzbuzz
{
class Program
{
static void Main(string[] args)
{
int[] fizzBuzzArr = new int [100]; // array set to 100 integers
int i = 0; // our counter to fill fizzBuzzArr
foreach(int value in fizzBuzzArr) // begin loop
{
i++;
Console.Write(i % 15 == 0 ? $"({i} fizzBuzz)\n\n" // initial if
: i % 3 == 0 ? $"({i} buzz), " // else if
: i % 5 == 0 ? $"({i} buzz)\n\n" // else if
: $"{i}, "); // else
} // end loop
Console.ReadLine();
}
}
}
Out put should look something like this.
1, 2, (3 buzz), 4, (5 buzz)
(6 buzz), 7, 8, (9 buzz), (10 buzz)
11, (12 buzz), 13, 14, (15 fizzBuzz)
16, 17, (18 buzz), 19, (20 buzz)
(21 buzz), 22, 23, (24 buzz), (25 buzz)
26, (27 buzz), 28, 29, (30 fizzBuzz)
31, 32, (33 buzz), 34, (35 buzz)
(36 buzz), 37, 38, (39 buzz), (40 buzz)
41, (42 buzz), 43, 44, (45 fizzBuzz)
46, 47, (48 buzz), 49, (50 buzz)
(51 buzz), 52, 53, (54 buzz), (55 buzz)
56, (57 buzz), 58, 59, (60 fizzBuzz)
61, 62, (63 buzz), 64, (65 buzz)
(66 buzz), 67, 68, (69 buzz), (70 buzz)
71, (72 buzz), 73, 74, (75 fizzBuzz)
76, 77, (78 buzz), 79, (80 buzz)
(81 buzz), 82, 83, (84 buzz), (85 buzz)
86, (87 buzz), 88, 89, (90 fizzBuzz)
91, 92, (93 buzz), 94, (95 buzz)
(96 buzz), 97, 98, (99 buzz), (100 buzz)
Hopefully this works, lol.
Top comments (3)
Nice solution, but remember there is nothing wrong with a good old fashioned for loop once in a while 😉.
Thanks. I'm looking back over it and realizing I could've done it with (i % 15 == 0 ) vs using &&. Always learning 😁.
I have some unneeded parentheses, as well. Might change it later or maybe I'll live it be as a reminder of growth
Made some minor adjustments