Related Articles
Menu-Driven program using Switch-case in C
• Difficulty Level : Medium
• Last Updated : 16 Jul, 2019

Prerequisite : Switch Case in C

Problem Statement:
Write a menu-driven program using Switch case to calculate the following:
1. Area of circle
2. Area of square
3. Area of sphere

Also use functions input() and output() to input and display respective values.

 `// C program to illustrate``// Menu-Driven program``// using Switch-case`` ` `#include ``int` `input();``void` `output(``float``);``int` `main()``{``    ``float` `result;``    ``int` `choice, num;``    ``printf``(``"Press 1 to calculate area of circle\n"``);``    ``printf``(``"Press 2 to calculate area of square\n"``);``    ``printf``(``"Press 3 to calculate area of sphere\n"``);``    ``printf``(``"Enter your choice:\n"``);``    ``choice = input();``     ` `    ``switch` `(choice) {``    ``case` `1: {``        ``printf``(``"Enter radius:\n"``);``        ``num = input();``        ``result = 3.14 * num * num;``        ``printf``(``"Area of sphere="``);``        ``output(result);``        ``break``;``    ``}``    ``case` `2: {``        ``printf``(``"Enter side of square:\n"``);``        ``num = input();``        ``result = num * num;``        ``printf``(``"Area of square="``);``        ``output(result);``        ``break``;``    ``}``    ``case` `3: {``        ``printf``(``"Enter radius:\n"``);``        ``num = input();``        ``result = 4 * (3.14 * num * num);``        ``printf``(``"Area of sphere="``);``        ``output(result);``        ``break``;``    ``}``    ``default``:``        ``printf``(``"wrong Input\n"``);``    ``}``    ``return` `0;``}``int` `input()``{``    ``int` `number;``    ``scanf``(``"%d"``, &number);``    ``return` `(number);``}`` ` `void` `output(``float` `number)``{``    ``printf``(``"%f"``, number);``}`

Output:

```Press 1 to calculate area of circle
Press 2 to calculate area of square
Press 3 to calculate area of sphere
Enter your choice:
1
Enter radius:
5
Area of circle=78.5
```

Related Articles:

Want to learn from the best curated videos and practice problems, check out the C Foundation Course for Basic to Advanced C.

My Personal Notes arrow_drop_up