0% found this document useful (0 votes)
15 views7 pages

Practical Assignment on Switch Case

Practical Assignment 04 (1)

Uploaded by

reetish.1410
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)
15 views7 pages

Practical Assignment on Switch Case

Practical Assignment 04 (1)

Uploaded by

reetish.1410
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

Practical Assignment: -3

Assignment on decision making statements (switch case)


1. Give the output of following code with explanation
a. #include<stdio.h>
#define L 10
void main()
{
auto a = 10; switch (a, a*2)
{
case L: printf("ABC"); break;
case L*2:
printf("XYZ");
break;
case L*3:
printf("PQR");
break;
default:
printf("MNO");
case L*4:
printf("www");
break;
}

Output🡪

Write down output here


[Link] a program to print grade of student using switch case
#include<stdio.h>
int main()
{
int marks;
/*C Program to Find Grade of a Student Using Switch Case*/
printf("\n-----------------------------------");
printf("\nEnter The Marks Between 0 To 100:");
printf("\nEnter The Mark: ");
scanf("%d", &marks);
if(marks>100)
{
/* Marks greater than 100 */
printf("\nDon't Be Smart Enter your Marks Between Limit\n");
}
else
{
switch(marks/10)
{
case 9 :
/* Marks between 90-100 */
printf("\n Your Grade is: A");
break;
case 8 :
/* Marks between 80-89 */
printf("\n Your Grade is: B" );
break;
case 7 :
/* Marks between 70-79 */
printf("\n Your Grade is: C" );
break;
case 6 :
/* Marks between 60-69 */
printf("\n Your Grade is: D" );
break;
case 5 :
/* Marks between 50-59 */
printf("\n Your Grade is: E" );
break;
case 4 :
/* Marks between 40-59 */
printf("\n Your Grade is: E--");
break;
default :
/* Marks less than 40 */
printf("\n You Grade is: F or Fail\n");
}
}
return 0;
}

Output:🡪

Enter The Marks Between 0 To 100:


Enter The Mark: 95
Your Grade is: A
[Link] to accept the week day as number from user and display Monday to
Sunday.
#include <stdio.h>
int main()
{
int week;

/* Input week number from user */


printf("Enter week number(1-7): ");
scanf("%d", &week);

switch(week)
{
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
case 4:
printf("Thursday");
break;
case 5:
printf("Friday");
break;
case 6:
printf("Saturday");
break;
case 7:
printf("Sunday");
break;
default:
printf("Invalid input! Please enter week number between 1-7.");
}

return 0;
}
Output🡪
Enter week number(1-7):
7
Sunday

[Link] to check whether a given character is VOWEL or CONSONANT


using switch-case

#include <stdio.h>
int main()
{
char ch;

/* Input an alphabet from user */


printf("Enter any alphabet: ");
scanf("%c", &ch);

/* Switch value of ch */
switch(ch)
{
case 'a':
printf("Vowel");
break;
case 'e':
printf("Vowel");
break;
case 'i':
printf("Vowel");
break;
case 'o':
printf("Vowel");
break;
case 'u':
printf("Vowel");
break;
case 'A':
printf("Vowel");
break;
case 'E':
printf("Vowel");
break;
case 'I':
printf("Vowel");
break;
case 'O':
printf("Vowel");
break;
case 'U':
printf("Vowel");
break;
default:
printf("Consonant");
}

return 0;
}
Output🡪

Enter any alphabet: A


Vowel

5. WAP to find number of days in a month using switch -case


#include <stdio.h>
int main()
{
int month;

/* Input month number from user */


printf("Enter month number(1-12): ");
scanf("%d", &month);

switch(month)
{
case 1:
printf("31 days");
break;
case 2:
printf("28/29 days");
break;
case 3:
printf("31 days");
break;
case 4:
printf("30 days");
break;
case 5:
printf("31 days");
break;
case 6:
printf("30 days");
break;
case 7:
printf("31 days");
break;
case 8:
printf("31 days");
break;
case 9:
printf("30 days");
break;
case 10:
printf("31 days");
break;
case 11:
printf("30 days");
break;
case 12:
printf("31 days");
break;
default:
printf("Invalid input! Please enter month number between 1-12");

return 0;
}
Output🡪
Enter month number(1-12): 4
30 days

Common questions

Powered by AI

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 .

You might also like