Menu Driven Program Using Switch Case in C

 

What is Switch Case?

The Switch statement is the type of statement which allows user to execute one code block among many alternatives.

We use a switch statement when we want to write 2 or more than 2 programs in a single program.

Inside the switch statement, cases are used we can number these cases using number or alphabets like case ‘a’: or case ‘1’:.After the case ends break statement should compulsorily be added.

To access a particular case we have to enter the case number After executing the program and the program written in that case starts executing.

when the user enters case number other than declared then default statement is used.

Flowchart of Switch Case:

switch case flow chart

Now we will learn how to write c program using switch statement using different types of examples.

1. Menu-driven Program Example 1

Write a menu-driven program to calculate 1. The Area of Circle 2. Area of Square 3. Area of Sphere

C

Output:

menu driven program in c

2. Menu Driven Program in C Example 2

Write a Menu Driven Program to find 1.Factorial of a number 2.Prime Number 3. Find Number is odd or even

C

Output:

menu driven program in c

In the above program when the case execution is complete the compiler again ask the user to enter the number to perform the operation.Because we have used while(1) in the starting of the program. And when we press 0 then the program is exited.

3. Menu Driven Program Using Function Example 3

Write a menu-driven program using a function which can do the following operations: 1. Addition of two numbers 2. Subtraction of two numbers 3. multiplication of two numbers 4. division of two numbers.

C

Output:

menu driven program in c using switch statement

In above program we have used function add() for addition,subtract() for subtraction,multiply() for multiplication and divide() for division.

No comments