c programs using "switch case"

 







1. write a c program print day of week name using switch...case?


Step by step descriptive logic to print day name of week.

  1. Input day number from user. Store it in some variable say week.
  2. Switch the value of week i.e. use switch(week) and match with cases.
  3. There can be 7 possible values(choices) of week i.e. 1 to 7. Therefore write 7 case inside switch. In addition, add default case as an else block.
  4. For case 1: print "MONDAY", for case 2: print "TUESDAY" and so on. Print "SUNDAY" for case 7:.
  5. If any case does not matches then, for default: case print "Invalid week number"

program



#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;
}

In the above program I have assumed "Monday" as the first day of week.

Output

Enter week number(1-7): 1
Monday

-----------------------------------------------------------------------------------------

2.C program to check vowel or consonant using switch case


English alphabets 'a', 'e', 'i', 'o', 'u' both lowercase and uppercase are known as vowels. Alphabets other than vowels are known as consonants.

Step by step descriptive logic to check vowel or consonant.

  1. Input an alphabet from user. Store it in some variable say ch.
  2. Switch the value of ch.
  3. For ch, there are 10 possibilities for vowel we need to check i.e. aeiouAEIO and U.
  4. Write all 10 possible cases for vowels and print "Vowel" for each case.
  5. If alphabet is not vowel then add a default case and print "Consonant".


#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; }



----------------------------------------------------------------------------------

3.C Program to Find Maximum of Two Numbers using Switch Case Statement


We will first take two numbers as input from user using scanf function. Then we print the maximum number on screen using switch case statement.


#include <stdio.h>
#include <conio.h> 
   
int main() { 
    int a, b; 
   
    /* Take two numbers as input from user
  using scanf function */
    printf("Enter Two Integers\n"); 
    scanf("%d %d", &a, &b); 
   
    switch(a > b) {    
        /* a>b comparison returns true(1)  */ 
        case 1: printf("%d is Maximum", a); 
                break
        /* a>b comparison returns false(0) */ 
        case 0: printf("%d is maximum", b); 
                break
    
     
    getch();
    return 0; 
}
Output
Enter Two Integers
4 8
8 is Maximum
Enter Two Integers
-2 -4
-2 is Maximum


------------------------------------------------------------------------------------------------------------------------


4.C Program to print grade of a student using Switch case statement


#include <stdio.h>
  
int main(){
   int marks;
    
   printf("Enter your marks between 0 to 100\n");
   scanf("%d", &marks);
    
   switch(marks/10){
       case 10 :
       case 9 :
           /* Marks between 90-100 */
           printf("Your Grade : A\n" );
           break;
       case 8 :
       case 7 :
           /* Marks between 70-89 */
           printf("Your Grade : B\n" );
           break;
       case 6 :
           /* Marks between 60-69 */
           printf("Your Grade : C\n" );
           break;
       case 5 :
       case 4 :
           /* Marks between 40-59 */
           printf("Your Grade : D\n" );
           break;
       default :
           /* Marks less than 40 */
           printf("You failed\n" );
   }
 
   return 0;
}
Above program check whether a student passed or failed in examination using if statement.

Output
Enter your marks between 0 to 100
55
Your Grade : D
Enter your marks between 0 to 100
99
Your Grade : A
Enter your marks between 0 to 100
30
You failed

--------------------------------------------------------------------------------------------------------------------------

5.Check EVEN or ODD program using switch


#include <stdio.h>
 
int main()
{
    int number;
     
    printf("Enter a positive integer number: ");
    scanf("%d",&number);
     
    switch(number%2) //this will return either 0 or 1
    {
        case 0:
            printf("%d is an EVEN number.\n",number);
            break;
        case 1:
            printf("%d is an ODD number.\n",number);
            break;
    }
     
    return 0;
}

Output

First run:
Enter a positive integer number: 10 
10 is an EVEN number. 

Second run:
Enter a positive integer number: 11 
11 is an ODD number.



----------------------------------------------------------------------------------------------------------------------------



6.C program to find number of days in a month using switch case


This program will read month value and print total number of days in input month in C language.

In this c program, we will learn how to get number of days in a month. To get number of days in a month we are using switch case statement in this program.

Here, we are not using break after each switch case statement because we can check multiple case values for same body.

Note: Here we are not checking leap year, so we fixed 28 days in February


#include <stdio.h>

int main()
{
	int month;
	int days;
	
	printf("Enter month: ");
	scanf("%d",&month);
	
	switch(month)
	{
		case  4:
		case  6:
		case  9:
		case 11:
			days=30;
			break;
		case  1:
		case  3:
		case  5:
		case  7:
		case  8:
		case 10:
		case 12:
			days=31;
			break;
			
		case 2:
			days=28;
			break;
		
		default:
			days=0;
			break;		
	}
	
	if(days)
		printf("Number of days in %d month is: %d\n",month,days);
	else
		printf("You have entered an invalid month!!!\n");
	
	return 0;
	
}

Output

First run:
Enter month: 3
Number of days in 3 month is: 31

Second run:
Enter month: 2
Number of days in 2 month is: 28

Third run:
Enter month: 11 
Number of days in 11 month is: 30 

Fourth run:
Enter month: 13 
You have entered an invalid month!!!




Comments

  1. Print gender (Male/Female) program according to given M/F using switch


    Program

    #include

    int main()
    {
    char gender;

    printf("Enter gender (M/m or F/f): ");
    scanf("%c",&gender);

    switch(gender)
    {
    case 'M':
    case 'm':
    printf("Male.");
    break;
    case 'F':
    case 'f':
    printf("Female.");
    break;
    default:
    printf("Unspecified Gender.");
    }
    printf("\n");
    return 0;
    }

    ReplyDelete
  2. C Program for Arithmetic Operations using Switch

    Program


    #include
    #include
    int main()
    {
    int a,b;
    int op;
    printf(" 1.Addition\n 2.Subtraction\n 3.Multiplication\n 4.Division\n");
    printf("Enter the values of a & b: ");
    scanf("%d %d",&a,&b);
    printf("Enter your Choice : ");
    scanf("%d",&op);
    switch(op)
    {
    case 1 :
    printf("Sum of %d and %d is : %d",a,b,a+b);
    break;
    case 2 :
    printf("Difference of %d and %d is : %d",a,b,a-b);
    break;
    case 3 :
    printf("Multiplication of %d and %d is : %d",a,b,a*b);
    break;
    case 4 :
    printf("Division of Two Numbers is %d : ",a/b);
    break;
    default :
    printf(" Enter Your Correct Choice.");
    break;
    }
    return 0;
    }


    Output :

    1.Addition
    2.Subtraction
    3.Multiplication
    4.Division
    Enter the values of a & b: 20 15
    Enter your Choice : 1
    Sum of 20 and 15 is : 35

    ReplyDelete

Post a Comment

cc