๐Ÿ  Session 9 Homework

Switch Statements - Menu Master

๐Ÿ“š What You'll Practice

This homework focuses on mastering switch statements, creating menu-driven programs, understanding case grouping, break statements, and default cases.

Goal: Become an expert in building interactive menu systems!

๐Ÿ“‹ Instructions

๐ŸŽฎ Program 1: Basic Switch Statement - Game Menu

Create a simple game selection menu!

What it does:

Demonstrates basic switch statement structure with case labels, break statements, and default case for menu selection.

#include <stdio.h> int main() { int game_choice; printf("=== GAME MENU ===\n"); printf("1. Minecraft Adventure\n"); printf("2. Roblox World\n"); printf("3. Fortnite Battle\n"); printf("4. Exit\n"); printf("Enter your choice (1-4): "); scanf("%d", &game_choice); switch (game_choice) { case 1: printf("๐ŸŽฎ Starting Minecraft Adventure...\n"); printf("Building your world! ๐Ÿ—๏ธ\n"); break; case 2: printf("๐ŸŽฎ Entering Roblox World...\n"); printf("Ready to explore! ๐ŸŒ\n"); break; case 3: printf("๐ŸŽฎ Launching Fortnite Battle...\n"); printf("Battle Royale mode! โš”๏ธ\n"); break; case 4: printf("๐Ÿ‘‹ Thanks for playing! Goodbye!\n"); break; default: printf("โŒ Invalid choice! Please select 1-4.\n"); } return 0; }

๐Ÿงช Test Cases

  • Choice 1 โ†’ Minecraft Adventure message
  • Choice 2 โ†’ Roblox World message
  • Choice 3 โ†’ Fortnite Battle message
  • Choice 4 โ†’ Exit message
  • Choice 5 โ†’ Invalid choice message

๐Ÿฝ๏ธ Program 2: Restaurant Menu System

Build a complete restaurant order system!

What it does:

Creates a restaurant menu using switch statements with prices and descriptions for each menu item.

#include <stdio.h> int main() { int menu_choice; int quantity; int total_price; printf("=== RESTAURANT MENU ===\n"); printf("1. Pizza - โ‚น200\n"); printf("2. Burger - โ‚น150\n"); printf("3. Samosa - โ‚น20\n"); printf("4. Dosa - โ‚น80\n"); printf("5. Biryani - โ‚น250\n"); printf("Enter your choice (1-5): "); scanf("%d", &menu_choice); printf("Enter quantity: "); scanf("%d", &quantity); switch (menu_choice) { case 1: total_price = 200 * quantity; printf("๐Ÿ• Pizza x%d ordered!\n", quantity); printf("Total: โ‚น%d\n", total_price); break; case 2: total_price = 150 * quantity; printf("๐Ÿ” Burger x%d ordered!\n", quantity); printf("Total: โ‚น%d\n", total_price); break; case 3: total_price = 20 * quantity; printf("๐ŸฅŸ Samosa x%d ordered!\n", quantity); printf("Total: โ‚น%d\n", total_price); break; case 4: total_price = 80 * quantity; printf("๐Ÿฅž Dosa x%d ordered!\n", quantity); printf("Total: โ‚น%d\n", total_price); break; case 5: total_price = 250 * quantity; printf("๐Ÿ› Biryani x%d ordered!\n", quantity); printf("Total: โ‚น%d\n", total_price); break; default: printf("โŒ Invalid menu choice! Please select 1-5.\n"); } return 0; }

๐Ÿงช Test Cases

  • Choice 1, Quantity 2 โ†’ Pizza x2, Total โ‚น400
  • Choice 3, Quantity 5 โ†’ Samosa x5, Total โ‚น100
  • Choice 5, Quantity 1 โ†’ Biryani x1, Total โ‚น250
  • Choice 6 โ†’ Invalid choice message

๐Ÿ”— Program 3: Case Grouping - Sports Categories

Learn to group cases for shared actions!

What it does:

Demonstrates case grouping where multiple case labels share the same action, making code more efficient and readable.

#include <stdio.h> int main() { int sports_choice; printf("=== SPORTS CATEGORY ===\n"); printf("1. Football\n"); printf("2. Cricket\n"); printf("3. Basketball\n"); printf("4. Tennis\n"); printf("5. Badminton\n"); printf("Enter your choice (1-5): "); scanf("%d", &sports_choice); switch (sports_choice) { case 1: case 2: case 3: printf("โšฝ Team Sport Selected!\n"); printf("These sports require teamwork! ๐Ÿ‘ฅ\n"); break; case 4: case 5: printf("๐ŸŽพ Individual Sport Selected!\n"); printf("These sports focus on individual skills! ๐Ÿ†\n"); break; default: printf("โŒ Invalid sport choice!\n"); } return 0; }

๐Ÿงช Test Cases

  • Choice 1, 2, or 3 โ†’ Team Sport message
  • Choice 4 or 5 โ†’ Individual Sport message
  • Choice 6 โ†’ Invalid choice message

๐ŸŽฏ Program 4: Complete Menu System

Build a comprehensive menu-driven program!

What it does:

Creates a complete menu system with multiple options, calculations, and user interactions using switch statements.

#include <stdio.h> int main() { int choice; int num1, num2, result; char operation; printf("=== CALCULATOR MENU ===\n"); printf("1. Addition\n"); printf("2. Subtraction\n"); printf("3. Multiplication\n"); printf("4. Division\n"); printf("5. Modulo\n"); printf("Enter your choice (1-5): "); scanf("%d", &choice); switch (choice) { case 1: printf("Enter two numbers: "); scanf("%d %d", &num1, &num2); result = num1 + num2; printf("Result: %d + %d = %d\n", num1, num2, result); break; case 2: printf("Enter two numbers: "); scanf("%d %d", &num1, &num2); result = num1 - num2; printf("Result: %d - %d = %d\n", num1, num2, result); break; case 3: printf("Enter two numbers: "); scanf("%d %d", &num1, &num2); result = num1 * num2; printf("Result: %d * %d = %d\n", num1, num2, result); break; case 4: printf("Enter two numbers: "); scanf("%d %d", &num1, &num2); if (num2 != 0) { result = num1 / num2; printf("Result: %d / %d = %d\n", num1, num2, result); } else { printf("โŒ Error: Division by zero!\n"); } break; case 5: printf("Enter two numbers: "); scanf("%d %d", &num1, &num2); if (num2 != 0) { result = num1 % num2; printf("Result: %d %% %d = %d\n", num1, num2, result); } else { printf("โŒ Error: Modulo by zero!\n"); } break; default: printf("โŒ Invalid choice! Please select 1-5.\n"); } return 0; }

๐Ÿงช Test Cases

  • Choice 1, Numbers 10, 5 โ†’ Result: 15
  • Choice 2, Numbers 20, 8 โ†’ Result: 12
  • Choice 3, Numbers 6, 7 โ†’ Result: 42
  • Choice 4, Numbers 15, 3 โ†’ Result: 5
  • Choice 4, Numbers 10, 0 โ†’ Error message
  • Choice 5, Numbers 17, 5 โ†’ Result: 2

๐Ÿ“… Program 5: Day of Week Classifier

Classify days using switch statements!

What it does:

Uses switch statements to classify days of the week into weekdays and weekends, demonstrating practical case grouping.

#include <stdio.h> int main() { int day; printf("=== DAY OF WEEK ===\n"); printf("1. Monday\n"); printf("2. Tuesday\n"); printf("3. Wednesday\n"); printf("4. Thursday\n"); printf("5. Friday\n"); printf("6. Saturday\n"); printf("7. Sunday\n"); printf("Enter day number (1-7): "); scanf("%d", &day); switch (day) { case 1: case 2: case 3: case 4: case 5: printf("๐Ÿ“… Weekday - Time to work! ๐Ÿ’ผ\n"); printf("Stay focused and productive!\n"); break; case 6: case 7: printf("๐ŸŽ‰ Weekend - Time to relax! ๐ŸŽฎ\n"); printf("Enjoy your free time!\n"); break; default: printf("โŒ Invalid day! Please enter 1-7.\n"); } return 0; }

๐Ÿงช Test Cases

  • Day 1-5 โ†’ Weekday message
  • Day 6-7 โ†’ Weekend message
  • Day 8 โ†’ Invalid day message