Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that outputs all 99 stanzas of the \"Ninety-Nine Bottles of Beer

ID: 3852052 • Letter: W

Question

Write a program that outputs all 99 stanzas of the "Ninety-Nine Bottles of Beer on the Wall" song. Your program should print the number of bottles in English, not as a number: Ninety-nine bottles of beer on the wall, Ninety-nine bottles of beer, Take one down, pass it around, Ninety-eight bottles of beer on the wall. .. One bottle of beer on the wall, One bottle of beer, Take one down, pass it around, Zero bottles of beer on the wall. Your program should not use ninety-nine different output statements!

Explanation / Answer

The c++ programming code for the program that outputs all 99 stanzas of "Ninenty-Nine Bottles of Beer on the Wall"song is:

#include <iostream.h>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::string;

string funcFirstDigit(int, int);
string funcSecondDigit(int);

int main()
{
   int firstDigit, secondDigit, bottles = 99;
   string line1, line2, line3, line4, altLine1, altLine2, altLine4, secondDigitText, ones, twenty, thirty, forty, fifty, sixty, seventy, eighty, ninety;
      
   altLine1 = " bottle of beer on the wall,"; //used when down to 1 bottle rather than use plural
   altLine2 = " bottle of beer,"; //used when down to 1 bottle rather than use plural
   line3 = "Take one down, pass it around,";
   altLine4 = " bottle of beer on the wall."; //used when down to 1 bottle rather than use plural
              
   for(int a = bottles; a >= 0; a--)
   {      
       firstDigit = bottles / 10; //get the first digit of the number of bottles remaining
       secondDigit = bottles % 10; //get the second digit of the number of bottles remaining
       line1 = " bottles of beer on the wall.";
       line2 = " bottles of beer,";
       line4 = " bottles of beer on the wall.";
              
       //specify correct text rather than a blank string when bottles reached zero
       if(bottles != 0) secondDigitText = funcSecondDigit(secondDigit);
       else secondDigitText = "Zero";
                                  
      
       switch(firstDigit)
       {
           case 0:
           {
               //use singular version if only 1 bottle left
               if(funcSecondDigit(secondDigit) == "One")
               {
                   line1 = altLine1;
                   line2 = altLine2;
               }
               cout << secondDigitText << line1 << endl << secondDigitText <<
               line2 << endl << line3 << endl << secondDigitText << line4 << endl;
               break;
           }
           case 1:
           {
               secondDigit);
               cout << ones << line1 << endl << ones << line2 << endl << line3 << endl << ones << line4 << endl;
               break;
           }
           //in cases 2 through 9, "if" statement required as we don't need the dash if the remaining bottles is divisible by 10
           case 2:
           {
               if(secondDigit == 0) twenty = "Twenty";
               else twenty = "Twenty-";
               cout << twenty << secondDigitText << line1 << endl << twenty << secondDigitText <<
               line2 << endl << line3 << endl << twenty << funcSecondDigit(secondDigit) << line4 << endl;
               break;
           }
           case 3:
           {
               if(secondDigit == 0) thirty = "Thirty";
               else thirty = "Thirty-";
               cout << thirty << secondDigitText << line1 << endl << thirty << secondDigitText <<
               line2 << endl << line3 << endl << thirty << secondDigitText << line4 << endl;
               break;
           }
           case 4:
           {
               if(secondDigit == 0) forty = "Forty";
               else forty = "Forty-";
               cout << forty << secondDigitText << line1 << endl << forty << secondDigitText <<
               line2 << endl << line3 << endl << forty << secondDigitText << line4 << endl;
               break;
           }
           case 5:
           {
               if(secondDigit == 0) fifty = "Fifty";
               else fifty = "Fifty-";
               cout << fifty << secondDigitText << line1 << endl << fifty << secondDigitText <<
               line2 << endl << line3 << endl << fifty << secondDigitText << line4 << endl;
               break;
           }
           case 6:
           {
               if(secondDigit == 0) sixty = "Sixty";
               else sixty = "Sixty-";
               cout << sixty << secondDigitText << line1 << endl << sixty << secondDigitText <<
               line2 << endl << line3 << endl << sixty << secondDigitText << line4 << endl;
               break;
           }
           case 7:
           {
               if(secondDigit == 0) seventy = "Seventy";
               else seventy = "Seventy-";
               cout << seventy << secondDigitText << line1 << endl << seventy << secondDigitText <<
               line2 << endl << line3 << endl << seventy << secondDigitText << line4 << endl;
               break;
           }
           case 8:
           {
               if(secondDigit == 0) eighty = "Eighty";
               else eighty = "Eighty-";
               cout << eighty << secondDigitText << line1 << endl << eighty << secondDigitText <<
               line2 << endl << line3 << endl << eighty << secondDigitText << line4 << endl;
               break;
           }
           case 9:
           {
               if(secondDigit == 0) ninety = "Ninety";
               else ninety = "Ninety-";
               cout << ninety << secondDigitText << line1 << endl << ninety << secondDigitText <<
               line2 << endl << line3 << endl << ninety << secondDigitText << line4 << endl;
               break;
           }
       }
                      
       bottles--;
       cout << endl;
   }
          
   return 0;
}

//bring in first digit from number of bottles remaining to see whether or check for special case of teens, etc
//it returns the digit in text form.  
string funcFirstDigit(int firstDigit, int secondDigit)
{
   string text;
      
   if(firstDigit == 1) //only need to do the following if the number of bottles left is between 10 and 19.
   {
       switch(secondDigit)
       {
           case 0: text = "Ten";
           break;
           case 1: text = "Eleven";
           break;
           case 2: text = "Twelve";
           break;
           case 3: text = "Thirteen";
           break;
           case 4: text = "Fourteen";
           break;
           case 5: text = "Fifteen";
           break;
           case 6: text = "Sixteen";
           break;
           case 7: text = "Seventeen";
           break;
           case 8: text = "Eighteen";
           break;
           case 9: text = "Nineteen";
       }
              
       return text;
   }
}
  
//brings in second digit from number of bottles remaining
//returns that digit in text form
string funcSecondDigit(int digit)
{
   string text;
  
   switch(digit)
   {
       case 0: text = "";
       break;
       case 1: text = "One";
       break;
       case 2:   text = "Two";
       break;
       case 3: text = "Three";
       break;
       case 4: text = "Four";
       break;
       case 5: text = "Five";
       break;
       case 6: text = "Six";
       break;
       case 7: text = "Seven";
       break;
       case 8: text = "Eight";
       break;
       case 9: text = "Nine";
   }
      
   return text;
}

the output is:

Ninety-Nine bottles of beer on the wall.
Ninety-Nine bottles of beer,
Take one down, pass it around,
Ninety-Nine bottles of beer on the wall.

Ninety-Eight bottles of beer on the wall.
Ninety-Eight bottles of beer,
Take one down, pass it around,
Ninety-Eight bottles of beer on the wall.

Ninety-Seven bottles of beer on the wall.
Ninety-Seven bottles of beer,
Take one down, pass it around,
Ninety-Seven bottles of beer on the wall.

Ninety-Six bottles of beer on the wall.
Ninety-Six bottles of beer,
Take one down, pass it around,
Ninety-Six bottles of beer on the wall.

Ninety-Five bottles of beer on the wall.
Ninety-Five bottles of beer,
Take one down, pass it around,
Ninety-Five bottles of beer on the wall.

Ninety-Four bottles of beer on the wall.
Ninety-Four bottles of beer,
Take one down, pass it around,
Ninety-Four bottles of beer on the wall.

Ninety-Three bottles of beer on the wall.
Ninety-Three bottles of beer,
Take one down, pass it around,
Ninety-Three bottles of beer on the wall.

Ninety-Two bottles of beer on the wall.
Ninety-Two bottles of beer,
Take one down, pass it around,
Ninety-Two bottles of beer on the wall.

Ninety-One bottles of beer on the wall.
Ninety-One bottles of beer,
Take one down, pass it around,
Ninety-One bottles of beer on the wall.

Ninety bottles of beer on the wall.
Ninety bottles of beer,
Take one down, pass it around,
Ninety bottles of beer on the wall.

Eighty-Nine bottles of beer on the wall.
Eighty-Nine bottles of beer,
Take one down, pass it around,
Eighty-Nine bottles of beer on the wall.

Eighty-Eight bottles of beer on the wall.
Eighty-Eight bottles of beer,
Take one down, pass it around,
Eighty-Eight bottles of beer on the wall.

Eighty-Seven bottles of beer on the wall.
Eighty-Seven bottles of beer,
Take one down, pass it around,
Eighty-Seven bottles of beer on the wall.

Eighty-Six bottles of beer on the wall.
Eighty-Six bottles of beer,
Take one down, pass it around,
Eighty-Six bottles of beer on the wall.

Eighty-Five bottles of beer on the wall.
Eighty-Five bottles of beer,
Take one down, pass it around,
Eighty-Five bottles of beer on the wall.

Eighty-Four bottles of beer on the wall.
Eighty-Four bottles of beer,
Take one down, pass it around,
Eighty-Four bottles of beer on the wall.

Eighty-Three bottles of beer on the wall.
Eighty-Three bottles of beer,
Take one down, pass it around,
Eighty-Three bottles of beer on the wall.

Eighty-Two bottles of beer on the wall.
Eighty-Two bottles of beer,
Take one down, pass it around,
Eighty-Two bottles of beer on the wall.

Eighty-One bottles of beer on the wall.
Eighty-One bottles of beer,
Take one down, pass it around,
Eighty-One bottles of beer on the wall.

Eighty bottles of beer on the wall.
Eighty bottles of beer,
Take one down, pass it around,
Eighty bottles of beer on the wall.

Seventy-Nine bottles of beer on the wall.
Seventy-Nine bottles of beer,
Take one down, pass it around,
Seventy-Nine bottles of beer on the wall.

Seventy-Eight bottles of beer on the wall.
Seventy-Eight bottles of beer,
Take one down, pass it around,
Seventy-Eight bottles of beer on the wall.

Seventy-Seven bottles of beer on the wall.
Seventy-Seven bottles of beer,
Take one down, pass it around,
Seventy-Seven bottles of beer on the wall.

Seventy-Six bottles of beer on the wall.
Seventy-Six bottles of beer,
Take one down, pass it around,
Seventy-Six bottles of beer on the wall.

Seventy-Five bottles of beer on the wall.
Seventy-Five bottles of beer,
Take one down, pass it around,
Seventy-Five bottles of beer on the wall.

Seventy-Four bottles of beer on the wall.
Seventy-Four bottles of beer,
Take one down, pass it around,
Seventy-Four bottles of beer on the wall.

Seventy-Three bottles of beer on the wall.
Seventy-Three bottles of beer,
Take one down, pass it around,
Seventy-Three bottles of beer on the wall.

Seventy-Two bottles of beer on the wall.
Seventy-Two bottles of beer,
Take one down, pass it around,
Seventy-Two bottles of beer on the wall.

Seventy-One bottles of beer on the wall.
Seventy-One bottles of beer,
Take one down, pass it around,
Seventy-One bottles of beer on the wall.

Seventy bottles of beer on the wall.
Seventy bottles of beer,
Take one down, pass it around,
Seventy bottles of beer on the wall.

Sixty-Nine bottles of beer on the wall.
Sixty-Nine bottles of beer,
Take one down, pass it around,
Sixty-Nine bottles of beer on the wall.

Sixty-Eight bottles of beer on the wall.
Sixty-Eight bottles of beer,
Take one down, pass it around,
Sixty-Eight bottles of beer on the wall.

Sixty-Seven bottles of beer on the wall.
Sixty-Seven bottles of beer,
Take one down, pass it around,
Sixty-Seven bottles of beer on the wall.

Sixty-Six bottles of beer on the wall.
Sixty-Six bottles of beer,
Take one down, pass it around,
Sixty-Six bottles of beer on the wall.

Sixty-Five bottles of beer on the wall.
Sixty-Five bottles of beer,
Take one down, pass it around,
Sixty-Five bottles of beer on the wall.

Sixty-Four bottles of beer on the wall.
Sixty-Four bottles of beer,
Take one down, pass it around,
Sixty-Four bottles of beer on the wall.

Sixty-Three bottles of beer on the wall.
Sixty-Three bottles of beer,
Take one down, pass it around,
Sixty-Three bottles of beer on the wall.

Sixty-Two bottles of beer on the wall.
Sixty-Two bottles of beer,
Take one down, pass it around,
Sixty-Two bottles of beer on the wall.

Sixty-One bottles of beer on the wall.
Sixty-One bottles of beer,
Take one down, pass it around,
Sixty-One bottles of beer on the wall.

Sixty bottles of beer on the wall.
Sixty bottles of beer,
Take one down, pass it around,
Sixty bottles of beer on the wall.

Fifty-Nine bottles of beer on the wall.
Fifty-Nine bottles of beer,
Take one down, pass it around,
Fifty-Nine bottles of beer on the wall.

Fifty-Eight bottles of beer on the wall.
Fifty-Eight bottles of beer,
Take one down, pass it around,
Fifty-Eight bottles of beer on the wall.

Fifty-Seven bottles of beer on the wall.
Fifty-Seven bottles of beer,
Take one down, pass it around,
Fifty-Seven bottles of beer on the wall.

Fifty-Six bottles of beer on the wall.
Fifty-Six bottles of beer,
Take one down, pass it around,
Fifty-Six bottles of beer on the wall.

Fifty-Five bottles of beer on the wall.
Fifty-Five bottles of beer,
Take one down, pass it around,
Fifty-Five bottles of beer on the wall.

Fifty-Four bottles of beer on the wall.
Fifty-Four bottles of beer,
Take one down, pass it around,
Fifty-Four bottles of beer on the wall.

Fifty-Three bottles of beer on the wall.
Fifty-Three bottles of beer,
Take one down, pass it around,
Fifty-Three bottles of beer on the wall.

Fifty-Two bottles of beer on the wall.
Fifty-Two bottles of beer,
Take one down, pass it around,
Fifty-Two bottles of beer on the wall.

Fifty-One bottles of beer on the wall.
Fifty-One bottles of beer,
Take one down, pass it around,
Fifty-One bottles of beer on the wall.

Fifty bottles of beer on the wall.
Fifty bottles of beer,
Take one down, pass it around,
Fifty bottles of beer on the wall.

Forty-Nine bottles of beer on the wall.
Forty-Nine bottles of beer,
Take one down, pass it around,
Forty-Nine bottles of beer on the wall.

Forty-Eight bottles of beer on the wall.
Forty-Eight bottles of beer,
Take one down, pass it around,
Forty-Eight bottles of beer on the wall.

Forty-Seven bottles of beer on the wall.
Forty-Seven bottles of beer,
Take one down, pass it around,
Forty-Seven bottles of beer on the wall.

Forty-Six bottles of beer on the wall.
Forty-Six bottles of beer,
Take one down, pass it around,
Forty-Six bottles of beer on the wall.

Forty-Five bottles of beer on the wall.
Forty-Five bottles of beer,
Take one down, pass it around,
Forty-Five bottles of beer on the wall.

Forty-Four bottles of beer on the wall.
Forty-Four bottles of beer,
Take one down, pass it around,
Forty-Four bottles of beer on the wall.

Forty-Three bottles of beer on the wall.
Forty-Three bottles of beer,
Take one down, pass it around,
Forty-Three bottles of beer on the wall.

Forty-Two bottles of beer on the wall.
Forty-Two bottles of beer,
Take one down, pass it around,
Forty-Two bottles of beer on the wall.

Forty-One bottles of beer on the wall.
Forty-One bottles of beer,
Take one down, pass it around,
Forty-One bottles of beer on the wall.

Forty bottles of beer on the wall.
Forty bottles of beer,
Take one down, pass it around,
Forty bottles of beer on the wall.

Thirty-Nine bottles of beer on the wall.
Thirty-Nine bottles of beer,
Take one down, pass it around,
Thirty-Nine bottles of beer on the wall.

Thirty-Eight bottles of beer on the wall.
Thirty-Eight bottles of beer,
Take one down, pass it around,
Thirty-Eight bottles of beer on the wall.

Thirty-Seven bottles of beer on the wall.
Thirty-Seven bottles of beer,
Take one down, pass it around,
Thirty-Seven bottles of beer on the wall.

Thirty-Six bottles of beer on the wall.
Thirty-Six bottles of beer,
Take one down, pass it around,
Thirty-Six bottles of beer on the wall.

Thirty-Five bottles of beer on the wall.
Thirty-Five bottles of beer,
Take one down, pass it around,
Thirty-Five bottles of beer on the wall.

Thirty-Four bottles of beer on the wall.
Thirty-Four bottles of beer,
Take one down, pass it around,
Thirty-Four bottles of beer on the wall.

Thirty-Three bottles of beer on the wall.
Thirty-Three bottles of beer,
Take one down, pass it around,
Thirty-Three bottles of beer on the wall.

Thirty-Two bottles of beer on the wall.
Thirty-Two bottles of beer,
Take one down, pass it around,
Thirty-Two bottles of beer on the wall.

Thirty-One bottles of beer on the wall.
Thirty-One bottles of beer,
Take one down, pass it around,
Thirty-One bottles of beer on the wall.

Thirty bottles of beer on the wall.
Thirty bottles of beer,
Take one down, pass it around,
Thirty bottles of beer on the wall.

Twenty-Nine bottles of beer on the wall.
Twenty-Nine bottles of beer,
Take one down, pass it around,
Twenty-Nine bottles of beer on the wall.

Twenty-Eight bottles of beer on the wall.
Twenty-Eight bottles of beer,
Take one down, pass it around,
Twenty-Eight bottles of beer on the wall.

Twenty-Seven bottles of beer on the wall.
Twenty-Seven bottles of beer,
Take one down, pass it around,
Twenty-Seven bottles of beer on the wall.

Twenty-Six bottles of beer on the wall.
Twenty-Six bottles of beer,
Take one down, pass it around,
Twenty-Six bottles of beer on the wall.

Twenty-Five bottles of beer on the wall.
Twenty-Five bottles of beer,
Take one down, pass it around,
Twenty-Five bottles of beer on the wall.

Twenty-Four bottles of beer on the wall.
Twenty-Four bottles of beer,
Take one down, pass it around,
Twenty-Four bottles of beer on the wall.

Twenty-Three bottles of beer on the wall.
Twenty-Three bottles of beer,
Take one down, pass it around,
Twenty-Three bottles of beer on the wall.

Twenty-Two bottles of beer on the wall.
Twenty-Two bottles of beer,
Take one down, pass it around,
Twenty-Two bottles of beer on the wall.

Twenty-One bottles of beer on the wall.
Twenty-One bottles of beer,
Take one down, pass it around,
Twenty-One bottles of beer on the wall.

Twenty bottles of beer on the wall.
Twenty bottles of beer,
Take one down, pass it around,
Twenty bottles of beer on the wall.

Nineteen bottles of beer on the wall.
Nineteen bottles of beer,
Take one down, pass it around,
Nineteen bottles of beer on the wall.

Eighteen bottles of beer on the wall.
Eighteen bottles of beer,
Take one down, pass it around,
Eighteen bottles of beer on the wall.

Seventeen bottles of beer on the wall.
Seventeen bottles of beer,
Take one down, pass it around,
Seventeen bottles of beer on the wall.

Sixteen bottles of beer on the wall.
Sixteen bottles of beer,
Take one down, pass it around,
Sixteen bottles of beer on the wall.

Fifteen bottles of beer on the wall.
Fifteen bottles of beer,
Take one down, pass it around,
Fifteen bottles of beer on the wall.

Fourteen bottles of beer on the wall.
Fourteen bottles of beer,
Take one down, pass it around,
Fourteen bottles of beer on the wall.

Thirteen bottles of beer on the wall.
Thirteen bottles of beer,
Take one down, pass it around,
Thirteen bottles of beer on the wall.

Twelve bottles of beer on the wall.
Twelve bottles of beer,
Take one down, pass it around,
Twelve bottles of beer on the wall.

Eleven bottles of beer on the wall.
Eleven bottles of beer,
Take one down, pass it around,
Eleven bottles of beer on the wall.

Ten bottles of beer on the wall.
Ten bottles of beer,
Take one down, pass it around,
Ten bottles of beer on the wall.

Nine bottles of beer on the wall.
Nine bottles of beer,
Take one down, pass it around,
Nine bottles of beer on the wall.

Eight bottles of beer on the wall.
Eight bottles of beer,
Take one down, pass it around,
Eight bottles of beer on the wall.

Seven bottles of beer on the wall.
Seven bottles of beer,
Take one down, pass it around,
Seven bottles of beer on the wall.

Six bottles of beer on the wall.
Six bottles of beer,
Take one down, pass it around,
Six bottles of beer on the wall.

Five bottles of beer on the wall.
Five bottles of beer,
Take one down, pass it around,
Five bottles of beer on the wall.

Four bottles of beer on the wall.
Four bottles of beer,
Take one down, pass it around,
Four bottles of beer on the wall.

Three bottles of beer on the wall.
Three bottles of beer,
Take one down, pass it around,
Three bottles of beer on the wall.

Two bottles of beer on the wall.
Two bottles of beer,
Take one down, pass it around,
Two bottles of beer on the wall.

One bottle of beer on the wall,
One bottle of beer,
Take one down, pass it around,
One bottles of beer on the wall.

Zero bottles of beer on the wall.
Zero bottles of beer,
Take one down, pass it around,
Zero bottles of beer on the wall.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote