In C language Switch in a Nested Loop Watch httpyoutubeb7cSZ
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!!!
Solution
#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(\"\ \\t\\t TWELVE DAYS OF CHRISTMAS \ \");
printf(\"\\t\\t_____________________________________\ \ \ \");
for (verse=1; verse<=days; verse++)
{
printf(\"\\tOn 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(\"\\t\\tTwelve Drummers Drumming\ \ \");break;
case 11: printf(\"\\t\\tEleven Pipers Piping\ \ \"); break;
case 10: printf(\"\\t\\tTen Lords a Leaping\ \ \");break;
case 9: printf(\"\\t\\tNine Ladies Dancing\ \ \"); break;
case 8: printf(\"\\t\\tEight Maids a Milking\ \ \");break;
case 7: printf(\"\\t\\tSeven Swans a Swimming\ \ \");break;
case 6: printf(\"\\t\\tSix Geese a Laying\ \ \");break;
case 5: printf(\"\\t\\tFive Golden Rings\ \ \");break;
case 4: printf(\"\\t\\tFour Calling Birds \ \ \");break;
case 3: printf(\"\\t\\tThree French Hens\ \ \");break;
case 2: printf(\"\\t\\tTwo Turtle Doves\ \ \");break;
case 1: printf(\"\\t\\t\");
if (verse > 1 )
printf(\"And \");
printf(\"A Partridge in a Pear Tree\ \ \");break;
// case 1: printf(\"\\t\\tA Partridge in a Pear Tree\ \ \");
}
}
}
return 0;
}
output:

