C Program switch case: "Nested Switch Case Example" with output
#include <stdio.h>
int main() { int mainChoice, subChoice; printf("Main Menu:\n"); printf("1. Fruit\n"); printf("2. Animal\n"); printf("Enter your choice: "); scanf("%d", &mainChoice); switch (mainChoice) { case 1: printf("Sub Menu - Fruit:\n"); printf("1. Apple\n"); printf("2. Banana\n"); printf("Enter your choice: "); scanf("%d", &subChoice); switch (subChoice) { case 1: printf("You selected Apple.\n"); break; case 2: printf("You selected Banana.\n"); break; default: printf("Invalid choice in the Fruit menu.\n"); break; } break; case 2: printf("Sub Menu - Animal:\n"); printf("1. Dog\n"); printf("2. Cat\n"); printf("Enter your choice: "); scanf("%d", &subChoice); switch (subChoice) { case 1: printf("You selected Dog.\n"); break; case 2: printf("You selected Cat.\n"); break; default: printf("Invalid choice in the Animal menu.\n"); break; } break; default: printf("Invalid choice in the Main Menu.\n"); break; } return 0; }Main Menu:
1. Fruit
2. Animal
Enter your choice: 1
Sub Menu - Fruit:
1. Apple
2. Banana
Enter your choice: 2
You selected Banana.
Comments
Post a Comment