0% found this document useful (0 votes)
11 views3 pages

Switch Lab 9

The document outlines a series of C++ programming tasks that involve using switch statements for various applications. Tasks include creating a simple calculator, a grade system, checking weekdays, identifying vowels or consonants, and implementing a menu-driven program for different functionalities. Additional tasks cover an ATM system, determining month days, converting uppercase to lowercase, displaying student result statuses, and managing traffic signal actions.

Uploaded by

Roohan Khan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Switch Lab 9

The document outlines a series of C++ programming tasks that involve using switch statements for various applications. Tasks include creating a simple calculator, a grade system, checking weekdays, identifying vowels or consonants, and implementing a menu-driven program for different functionalities. Additional tasks cover an ATM system, determining month days, converting uppercase to lowercase, displaying student result statuses, and managing traffic signal actions.

Uploaded by

Roohan Khan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Q1.

Simple Calculator

Write a C++ program that takes two numbers and an operator (+ , - , * , /) from the user and
uses a switch statement to perform the selected operation.

Q2. Grade System

Write a C++ program that accepts a character grade (A, B, C, D, F) and displays the
corresponding percentage using a switch statement.
Example:

 A → 90%

 B → 80%

 C → 70%

 D → 60%

 F → Fail

Q3. Week Days

Write a C++ program that takes a number (1–7) and prints the corresponding day of the week
using a switch statement.

Q4. Vowel or Consonant

Write a C++ program that takes a character from the user and uses a switch statement to check
whether the character is a vowel or a consonant.

Q5. Menu-Driven Program

Write a menu-driven C++ program using switch that allows the user to:

1. Check Even or Odd

2. Check Positive or Negative

3. Find Square of a Number


Q6. Simple ATM System

Write a C++ program using a switch statement that shows the following menu:

1. Check Balance

2. Withdraw Amount

3. Deposit Amount

4. Exit

Perform the selected operation accordingly.

Q7. Month Days

Write a C++ program that takes a month number (1–12) and displays the number of days in that
month using a switch statement.

Q8. Uppercase to Lowercase

Write a C++ program that takes an uppercase alphabet and converts it into lowercase using a
switch statement.

Q9. Student Result Status

Write a C++ program using switch that takes a result status code:

 P → Pass

 F → Fail

 A → Absent

Display the appropriate message.

Q10. Traffic Signal System


Write a C++ program that takes a traffic signal color (R, Y, G) and displays the appropriate action
using a switch statement.

 R → Stop

 Y → Ready

 G → Go

Common questions

Powered by AI

A C++ program can determine the appropriate action based on different traffic signal colors by using a switch statement to evaluate the input color ('R', 'Y', 'G'). Each color corresponds to an action: 'R' for Stop, 'Y' for Ready, and 'G' for Go. The program matches the input to these cases and displays the respective instruction, facilitating traffic management .

The use of switch statements facilitates month-to-day conversions in C++ programs by allowing each month to be a case in the statement. Each case returns the number of days corresponding to that month (e.g., 31 for January, 30 for April). This structure provides a clear and efficient way to manage different outputs for each month number input by the user .

A menu-driven C++ program designed with a switch statement offers functionalities such as checking if a number is even or odd, determining if it's positive or negative, and calculating the square of a number. These options are presented in a menu format, allowing users to select their desired operation, which is then executed based on the corresponding case in the switch statement .

A C++ program can convert an uppercase letter to lowercase using a switch statement by assigning each uppercase letter as a case and mapping it to its corresponding lowercase character. This direct mapping can include cases such as 'A' to 'a', 'B' to 'b', and so forth, ensuring each uppercase character is converted to its equivalent lowercase form .

In a simple ATM system using switch statements, different user actions are handled by mapping each action to a specific case in the switch statement. The menu might present options such as checking the balance, withdrawing money, depositing funds, or exiting. When a user selects an option, the switch statement directs the program to execute the corresponding set of instructions for that operation, facilitating the desired banking action .

In C++ programs, day-of-the-week outputs are managed using switch statements by associating each number from 1 to 7 with a specific case representing a weekday. For instance, 1 maps to Monday, 2 to Tuesday, and so on, culminating in a fixed output for each day, making it straightforward to convert numeric input into human-readable form .

A C++ program can use a switch statement to determine if a character input is a vowel or consonant by evaluating the character against specific cases for vowels and providing a default case. For instance, if a character is 'A', 'E', 'I', 'O', or 'U' (uppercase or lowercase), the program would identify it as a vowel. Otherwise, it would default to marking the character as a consonant .

The default case in switch statements serves as a catch-all for any input that does not match the specified cases. It is crucial for handling unexpected values and ensuring the program can provide a meaningful response or error message, thus enhancing robustness and user experience by covering all possible inputs not predefined in other cases .

A switch statement is a suitable choice for implementing grade systems in C++ because it provides a clear and maintainable structure for mapping character grades to percentage values. Each grade (A, B, C, D, F) can be a separate case, with the program executing a specific output for each case, like 90% for 'A', allowing for straightforward updates and readability .

A C++ programmer should prefer using switch statements over if-else structures when there is a need to evaluate a single variable or expression against a set of discrete values. Switch statements provide cleaner code and usually better performance for such scenarios due to their label-based branching, which is typically faster as it avoids checking multiple conditions like in if-else chains .

You might also like