In C language!!! Switch in a Nested Loop. Watch http://youtu.be/b7cSZD1GPuU If t
ID: 3937485 • Letter: I
Question
In C language!!!
Switch in a Nested Loop. Watch http://youtu.be/b7cSZD1GPuU If that makes you too queasy, try Bob and Doug's version: http://youtu.be/32CwrOZVobo Write a program that prompts the user for the number of days, then print the lyrics for the entire song for that number of days. Your program must work for any day between 1 and 12. You can use either the standard version or Bob and Doug's version. (You can google the lyrics). Use MUST use a nested loop, here is some partial code:
// get number of days from user
for (verse = 1; verse <= days; verse++)
{
// counting backwards thru the days of xmas
for (line = verse; line >= 1; line--)
{
// switch on line goes here
// each case will have a printf for that line
}
}
Sample Output:
Please enter how many days: 2
On day 1 of Christmas my true love gave to me,
A partridge in a pear tree
On day 2 of Christmas my true love gave to me,
Two turtle doves, and
A partridge in a pear tree
Press any key to continue . . .
You must support all 12 days!!!
Explanation / Answer
#include <stdio.h>
int main(void)
{
int days,verse,line;
// get number of days from user
printf(" Enter the number of days");
scanf("%d",&days);
printf(" TWELVE DAYS OF CHRISTMAS ");
printf(" _____________________________________ ");
for (verse=1; verse<=days; verse++)
{
printf(" On the ");
switch(verse)
{
case 1:
printf("1st");
break;
case 2:
printf("2nd");
break;
case 3:
printf("3rd");
break;
default:
printf("%dth", verse);
break;
}
printf(" day of Christmas my true love sent to me ");
for (line = verse; line >= 1; line--)
{
switch(line)
{
case 12: printf(" Twelve Drummers Drumming ");break;
case 11: printf(" Eleven Pipers Piping "); break;
case 10: printf(" Ten Lords a Leaping ");break;
case 9: printf(" Nine Ladies Dancing "); break;
case 8: printf(" Eight Maids a Milking ");break;
case 7: printf(" Seven Swans a Swimming ");break;
case 6: printf(" Six Geese a Laying ");break;
case 5: printf(" Five Golden Rings ");break;
case 4: printf(" Four Calling Birds ");break;
case 3: printf(" Three French Hens ");break;
case 2: printf(" Two Turtle Doves ");break;
case 1: printf(" ");
if (verse > 1 )
printf("And ");
printf("A Partridge in a Pear Tree ");break;
// case 1: printf(" A Partridge in a Pear Tree ");
}
}
}
return 0;
}
output: