How to resolve the algorithm 99 bottles of beer step by step in the C# programming language
How to resolve the algorithm 99 bottles of beer step by step in the C# programming language
Table of Contents
Problem Statement
Display the complete lyrics for the song: 99 Bottles of Beer on the Wall.
The lyrics follow this form: ... and so on, until reaching 0 (zero). Grammatical support for 1 bottle of beer is optional. As with any puzzle, try to do it in as creative/concise/comical a way as possible (simple, obvious solutions allowed, too).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm 99 bottles of beer step by step in the C# programming language
Program 1: This program uses a loop to print the lyrics of the song "99 Bottles of Beer on the Wall" in a straightforward manner. When the number of bottles reaches 0, it prints a special message to indicate that no more bottles are left.
Program 2: This program uses a lambda expression to create a function that generates a line of the song for a given number of bottles. It uses this function to print the lyrics in a concise and readable format.
Program 3: This program uses the Select, Linq, and ForEach methods to create a list of the lyrics and then print it. It uses lambda expressions to define the logic for generating each line of the song.
Program 4: This program defines several string constants and functions to generate the lyrics. It uses a while loop to repeatedly print the lyrics until the number of bottles reaches 0.
Program 5: This program defines a recursive function called beer to print the lyrics in a similar way to the previous program. It handles the case of one bottle specially to avoid grammatical issues.
Program 6: This program uses the BeerBottles method to generate a sequence of the lyrics and then takes the first 99 lines and prints them. The BeerBottles method uses a lamda expression to define the logic for generating each line.
Program 7: This program defines an array of beer names and uses a for loop to iterate through them, printing the lyrics with each name. It handles the case of one bottle specially to avoid grammatical issues.
Program 8: This program defines constant strings and integers to represent the lyrics and uses a loop to print them. It handles the case of one bottle specially to avoid grammatical issues.
Program 9: This program defines a recursive function called BottlesSong to print the lyrics in a similar way to Program 5. It handles the case of zero bottles specially.
Program 10: This program uses a while loop to print the lyrics in a straightforward manner similar to Program 1. It handles the case of one bottle specially to avoid grammatical issues.
Source code in the csharp programming language
using System;
class Program
{
static void Main(string[] args)
{
for (int i = 99; i > -1; i--)
{
if (i == 0)
{
Console.WriteLine("No more bottles of beer on the wall, no more bottles of beer.");
Console.WriteLine("Go to the store and buy some more, 99 bottles of beer on the wall.");
break;
}
if (i == 1)
{
Console.WriteLine("1 bottle of beer on the wall, 1 bottle of beer.");
Console.WriteLine("Take one down and pass it around, no more bottles of beer on the wall.");
Console.WriteLine();
}
else
{
Console.WriteLine("{0} bottles of beer on the wall, {0} bottles of beer.", i);
Console.WriteLine("Take one down and pass it around, {0} bottles of beer on the wall.", i - 1);
Console.WriteLine();
}
}
}
}
using System;
class Program
{
static void Main()
{
Func<int, bool, string> f = (x, y) =>
$"{(x == 0 ? "No more" : x.ToString())} bottle{(x == 1 ? "" : "s")} of beer{(y ? " on the wall" : "")}\r\n";
for (int i = 99; i > 0; i--)
Console.WriteLine($"{f(i, true)}{f(i, false)}Take one down, pass it around\r\n{f(i - 1, true)}");
}
}
using System;
using System.Linq;
class Program
{
static void Main()
{
Enumerable.Range(1, 99).Reverse().Select(x =>
$"{x} bottle{(x == 1 ? "" : "s")} of beer on the wall, {x} bottle{(x == 1 ? "" : "s")} of beer!\n" +
$"Take {(x == 1 ? "it" : "one")} down, pass it around, {(x == 1 ? "no more" : (x - 1).ToString())} bottles of beer on the wall!\n"
).ToList().ForEach(x => Console.WriteLine(x));
}
}
using System;
using System.Globalization;
class Program
{
const string Vessel = "bottle";
const string Beverage = "beer";
const string Location = "on the wall";
private static string DefaultAction(ref int bottles)
{
bottles--;
return "take one down, pass it around,";
}
private static string FallbackAction(ref int bottles)
{
bottles += 99;
return "go to the store, buy some more,";
}
private static string Act(ref int bottles)
{
return bottles > 0 ? DefaultAction(ref bottles) : FallbackAction(ref bottles);
}
static void Main()
{
Func<int, string> plural = b => b == 1 ? "" : "s";
Func<int, string> describeCount = b => b == 0 ? "no more" : b.ToString();
Func<int, string> describeBottles = b => string.Format("{0} {1}{2} of {3}", describeCount(b), Vessel, plural(b), Beverage);
Action<string> write = s => Console.WriteLine(CultureInfo.CurrentCulture.TextInfo.ToTitleCase(s));
int bottles = 99;
while (true)
{
write(string.Format("{0} {1}, {0},", describeBottles(bottles), Location));
write(Act(ref bottles));
write(string.Format("{0} {1}.", describeBottles(bottles), Location));
write(string.Empty);
}
}
}
class songs
{
static void Main(string[] args)
{
beer(3);
}
private static void beer(int bottles)
{
for (int i = bottles; i > 0; i--)
{
if (i > 1)
{
Console.Write("{0}\n{1}\n{2}\n{3}\n\n",
i + " bottles of beer on the wall",
i + " bottles of beer",
"Take one down, pass it around",
(i - 1) + " bottles of beer on the wall");
}
else
Console.Write("{0}\n{1}\n{2}\n{3}\n\n",
i + " bottle of beer on the wall",
i + " bottle of beer",
"Take one down, pass it around",
(i - 1) + " bottles of beer on the wall....");
}
}
}
using System;
using System.Linq;
class Program
{
static void Main()
{
BeerBottles().Take(99).ToList().ForEach(Console.WriteLine);
}
static IEnumerable<String> BeerBottles()
{
int i = 100;
String f = "{0}, {1}. Take one down, pass it around, {2}";
Func<int, bool, String> booze = (c , b) =>
String.Format("{0} bottle{1} of beer{2}", c>0 ? c.ToString() : "no more", (c==1 ? "" : "s"), b ? " on the wall" : "");
while (--i >= 1)
yield return String.Format(f, booze(i, true), booze(i, false), booze(i - 1, true));
}
}
string[] bottles = { "80 Shilling",
"Abita Amber",
"Adams Broadside Ale",
"Altenmünster Premium",
"August Schell's SnowStorm",
"Bah Humbug! Christmas Ale",
"Beck's Oktoberfest",
"Belhaven Wee Heavy",
"Bison Chocolate Stout",
"Blue Star Wheat Beer",
"Bridgeport Black Strap Stout",
"Brother Thelonius Belgian-Style Abbey Ale",
"Capital Blonde Doppelbock",
"Carta Blanca",
"Celis Raspberry Wheat",
"Christian Moerlein Select Lager",
"Corona",
"Czechvar",
"Delirium Tremens",
"Diamond Bear Southern Blonde",
"Don De Dieu",
"Eastside Dark",
"Eliot Ness",
"Flying Dog K-9 Cruiser Altitude Ale",
"Fuller's London Porter",
"Gaffel Kölsch",
"Golden Horseshoe",
"Guinness Pub Draught",
"Hacker-Pschorr Weisse",
"Hereford & Hops Black Spring Double Stout",
"Highland Oatmeal Porter",
"Ipswich Ale",
"Iron City",
"Jack Daniel's Amber Lager",
"Jamaica Sunset India Pale Ale",
"Killian's Red",
"König Ludwig Weiss",
"Kronenbourg 1664",
"Lagunitas Hairy Eyball Ale",
"Left Hand Juju Ginger",
"Locktender Lager",
"Magic Hat Blind Faith",
"Missing Elf Double Bock",
"Muskoka Cream Ale ",
"New Glarus Cherry Stout",
"Nostradamus Bruin",
"Old Devil",
"Ommegang Three Philosophers",
"Paulaner Hefe-Weizen Dunkel",
"Perla Chmielowa Pils",
"Pete's Wicked Springfest",
"Point White Biere",
"Prostel Alkoholfrei",
"Quilmes",
"Rahr's Red",
"Rebel Garnet",
"Rickard's Red",
"Rio Grande Elfego Bock",
"Rogue Brutal Bitter",
"Roswell Alien Amber Ale",
"Russian River Pliny The Elder",
"Samuel Adams Blackberry Witbier",
"Samuel Smith's Taddy Porter",
"Schlafly Pilsner",
"Sea Dog Wild Blueberry Wheat Ale",
"Sharp's",
"Shiner 99",
"Sierra Dorada",
"Skullsplitter Orkney Ale",
"Snake Chaser Irish Style Stout",
"St. Arnold Bock",
"St. Peter's Cream Stout",
"Stag",
"Stella Artois",
"Stone Russian Imperial Stout",
"Sweetwater Happy Ending Imperial Stout",
"Taiwan Gold Medal",
"Terrapin Big Hoppy Monster",
"Thomas Hooker American Pale Ale",
"Tie Die Red Ale",
"Toohey's Premium",
"Tsingtao",
"Ugly Pug Black Lager",
"Unibroue Qatre-Centieme",
"Victoria Bitter",
"Voll-Damm Doble Malta",
"Wailing Wench Ale",
"Warsteiner Dunkel",
"Wellhead Crude Oil Stout",
"Weyerbacher Blithering Idiot Barley-Wine Style Ale",
"Wild Boar Amber",
"Würzburger Oktoberfest",
"Xingu Black Beer",
"Yanjing",
"Younger's Tartan Special",
"Yuengling Black & Tan",
"Zagorka Special",
"Zig Zag River Lager",
"Zywiec" };
int bottlesLeft = 99;
const int FIRST_LINE_SINGULAR = 98;
const int FINAL_LINE_SINGULAR = 97;
string firstLine = "";
string finalLine = "";
for (int i = 0; i < 99; i++)
{
firstLine = bottlesLeft.ToString() + " bottle";
if (i != FIRST_LINE_SINGULAR)
firstLine += "s";
firstLine += " of beer on the wall, " + bottlesLeft.ToString() + " bottle";
if (i != FIRST_LINE_SINGULAR)
firstLine += "s";
firstLine += " of beer";
Console.WriteLine(firstLine);
Console.WriteLine("Take the " + bottles[i] + " down, pass it around,");
bottlesLeft--;
finalLine = bottlesLeft.ToString() + " bottle";
if (i != FINAL_LINE_SINGULAR)
finalLine += "s";
finalLine += " of beer on the wall!";
Console.WriteLine(finalLine);
Console.WriteLine();
Console.ReadLine();
}
public static void BottlesSong(int numberOfBottles)
{
if (numberOfBottles > 0)
{
Console.WriteLine("{0} bottles of beer on the wall", numberOfBottles);
Console.WriteLine("{0} bottles of beer ", numberOfBottles);
Console.WriteLine("Take one down, pass it around");
Console.WriteLine("{0} bottles of beer ", numberOfBottles - 1);
Console.WriteLine();
BottlesSong(--numberOfBottles);
}
}
static void Main(string[] args)
{
int numBottles = 99;
while (numBottles > 0)
{
if (numBottles > 1)
{
WriteLine("{0} bottles of beer on the wall, {0} bottles of beer.", numBottles);
numBottles -= 1;
WriteLine("Take one down, pass it around, {0} bottles of beer on the wall.\n", numBottles);
}
else
{
WriteLine("{0} bottle of beer on the wall, {0} bottle of beer.", numBottles);
numBottles -= 1;
WriteLine("Take one down, pass it around, no more bottles of beer on the wall.\n");
}
}
WriteLine("No more bottles of beer on the wall, no more bottles of beer.");
WriteLine("Go to the store to buy some more, 99 bottles of beer on the wall...");
}
You may also check:How to resolve the algorithm Smith numbers step by step in the Delphi programming language
You may also check:How to resolve the algorithm Pangram checker step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Floyd-Warshall algorithm step by step in the Python programming language
You may also check:How to resolve the algorithm Abbreviations, simple step by step in the Wren programming language
You may also check:How to resolve the algorithm Langton's ant step by step in the AutoIt programming language