Practical Assignment on Switch Case
Practical Assignment on Switch Case
To improve accuracy and completeness, the month-days program could incorporate a check for leap years to determine February's correct length dynamically. Adding input validation to ensure numerical inputs and appropriate feedback for inputs outside 1-12 can enhance usability .
The program uses a switch-case structure to match each month number with its corresponding case, which holds the number of days. For February, it specifies "28/29 days" without involving leap year calculations; other months are handled with their respective constant day values, e.g., "30 days" for April, June, September, and November, and "31 days" for other months .
The code fails to handle invalid week numbers below 1 because it directly triggers the `default` case that handles inputs outside the range 1-7. This case outputs "Invalid input! Please enter week number between 1-7," highlighting oversight in checking inputs less than 1 before processing them in the `switch` statement .
The program does not explicitly handle non-alphabetic inputs such as numbers or symbols, and these would be categorized as 'Consonant' by default, as they don't match any of the specified vowel cases. This presents a potential area for enhancement by incorporating such input checks .
The program uses a switch-case statement where each case corresponds to a day number, with the mapping order of `1` to `Monday`, `2` to `Tuesday`, through `7` to `Sunday`. The `break` statement after each print ensures that only the matched day is outputted .
The output of the code is 'MNOwww'. The `switch (a, a*2)` evaluates to `switch (20)`. For the `switch` statement, none of the `case` values (which are `10`, `20`, `30`) are matched, so it defaults to printing 'MNO'. Since there is no `break;` in the `default:` case, execution continues to `case L*4`, which is `case 40`, printing 'www' as well .
The switch-case statement manages grades by dividing the input marks by 10 and matching the quotient to a case. For instance, a quotient of 9 corresponds to marks between 90-100, assigns 'A'; 8 for 80-89 assigns 'B'; and similarly, other numbers are used for assigning grades 'C', 'D', 'E', and 'F' for lower scores .
For an input of 0, the program outputs "Invalid input! Please enter week number between 1-7," since 0 does not map to any of the defined cases from `1` to `7`, causing the default case to execute .
The program effectively distinguishes between vowels and consonants by explicitly listing both lowercase and uppercase vowels in separate `case` branches, falling to the `default` case for consonants. The comprehensive handling of vowels ensures accuracy regardless of input case, making the solution robust for this classification task .
The program handles grades outside the valid range by checking if the input marks exceed 100 and outputs a warning message "Don't Be Smart Enter your Marks Between Limit." However, it does not explicitly handle inputs below zero .