Translate

important

What is a switch case statement in C language?| C language switch case programs example

The switch case statement in C is an alternative of the if else if ladder statement which allows us to execute multiple operations for the different possible values of a single variable called switch variable. Here we can define various statements in the multiple cases for the different values of a single variable.

C language switch case programs example


C language Switch case Syntax

switch(expression)

case value 1:

 //code to be executed; 

break;//optional 

case value 2: 

//code to be executed; 

break;//optional 

………. 

default://optional 

//code to be executed if all cases are not matched; 

}

Rules for switch statement in C

  1. switch expression must be of an integer or character type.
  2. case value must be an integer or character constant.
  3. break statement in the switch case is not must but optional,if there is no break statement found in the case,all the cases will be executed present after the matched case and it is known as "fall through" the state of the C switch statement.

Some important question and solutions from C switch case statement or switch case practice questions in C.

Q.1: Write a program to accept year and month and display the number of days in given month.

Ans:

#include<stdio.h>
int main()
{
int yr,m;
printf("Enter Year:");
scanf("%d",&yr);
printf("\nEnter Month:");
scanf("%d",&m);
switch(m)
{
case 1:
printf("January:31days\n");
break;
case 2:
if(yr%4==0)
  {
  if(yr%100==0)
    {
    if(yr%400==0)
      {
     printf("Fabruary:29 days\n");
      }
    else
      {
    printf("February:28 days\n");
      }
    }
  else
    {
  printf("February:29 days\n");
    }
}
else
{
printf("February:28 days");
}
break;
case 3:
printf("March:31 days\n");
break;
case 4:
printf("April:30 days\n");
break;
case 5:
printf("May:31 days\n");
break;
case 6:
printf("June:30 days\n");
break;
case 7:
printf("July:31 days\n");
break;
case 8:
printf("August:31 days\n");
break;
case 9:
printf("September:30 days\n");
break;
case 10:
printf("October:31 days\n");
break;
case 11:
printf("November:30 days\n");
break;
case 12:
printf("December:31 days\n");
break;
default:
printf("Wrong choice");
}
return 0;
}

OUTPUT: Enter Year: 2016
                Enter month: 2
                February: 29 days

Q.2: Write a program to print day of a week by accepting values between 1-7.

Ans:

#include<stdio.h>
int main()
{
int day;
printf("Enter Day between 1 to 7:");
scanf("%d",&day);
switch(day)
{
case 1:
printf("Sunday\n");
break;
case 2:
printf("Monday\n");
break;
case 3:
printf("Tuesday\n");
break;
case 4:
printf("Wednesday\n");
break;
case 5:
printf("Thursday\n");
break;
case 6:
printf("Friday\n");
break;
case 7:
printf("Saturday\n");
break;
default:
printf("Wrong choice");
}
return 0;
}

OUTPUT: Enter Day between 1 to 7: 2
                Monday

Q.3: Write a C program to check whether an alphabet is vowel or consonant using switch case.

Ans:

#include<stdio.h>
int main()
{
char alp;
printf("Enter an alphabet:");
scanf("%c",&alp);
switch(alp)
{
case 'A':
printf("%c is Vowel",alp);
break;
case 'E':
printf("%c is Vowel",alp);
break;
case 'I':
printf("%c is Vowel",alp);
break;
case 'O':
printf("%c is Vowel",alp);
break;
case 'U':
printf("%c is Vowel",alp);
break;
case 'a':
printf("%c is Vowel",alp);
break;
case 'e':
printf("%c is Vowel",alp);
break;
case 'i':
printf("%c is Vowel",alp);
break;
case 'o':
printf("%c is Vowel",alp);
break;
case 'u':
printf("%c is Vowel",alp);
break;
default:
printf("%c is Consonant",alp);
}
return 0;
}

OUTPUT: Enter an alphabet:E
                E is Vowel

Q.4: Write a C program to find maximum between two numbers using switch case.

Ans:

#include<stdio.h>
int main()
{
int a,b;
printf("Enter two numbers\n");
scanf("%d %d",&a,&b);
switch(a==b)
{
case 1:
printf("%d and %d is equal",a,b);
break;
case 0:
 switch(a>b)
   {
   case 1:
   printf("%d is maximum number",a);
   break;
   case 0:
   printf("%d is maximum number",b);
   break;
   }
break;
default:
printf("Wrong choice");
}
return 0;
}

OUTPUT: Enter two numbers
                25
                24
                25 is maximum number

Q.5: Write a C program to check whether a number is even or odd using switch case.

Ans:

#include<stdio.h>
int main()
{
int a;
printf("Enter a number:");
scanf("%d",&a);
switch(a%2==0)
{
case 1:
printf("%d is even number",a);
break;
case 0:
printf("%d is odd number",a);
break;
default:
printf("Wrong choice");
}
return 0;
}

OUTPUT:Enter a number:25
               25 is odd number

so,this is switch statement in C and some Questions from switch case statement in C, I hope you like this article,if you have any doubt or question regarding this than you can comment below without signing with google account so don't hesitate and comment freely....! 

Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Top Post Ad

Below Post Ad