C Language | Switch Statement examples

Question:1

Write a C program to take input for a character check and print whether it is a vowel or not?

Sol:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char ch;
    printf("Enter any character ");
    scanf("%c",&ch);
    switch(ch)
    {
    case 'A':
    case 'a':
    case 'E':
    case 'e':
    case 'I':
    case 'i':
    case 'O':
    case 'o':
    case 'u':
    case 'U':
        printf("it is a vowel");
        break;
    default:
        printf("It is not a vowel");
        break;
    }
    return 0;
}

Output:

case 1:

Enter any character +
It is not a vowel

case 2:

Enter any character A
it is a vowel

case 3:

Enter any character e
it is a vowel

Question:2

Write a C program to take input for a character check and print whether it is a vowel or a consonant?

Sol:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char ch;
    printf("Enter any character ");
    scanf("%c",&ch);
    if((ch>='A' && ch<='Z') || (ch>='a' && ch<='z'))
    {
        printf("it is an alphabet\n");
        switch(ch)
        {
            case 'A':
            case 'a':
            case 'E':
            case 'e':
            case 'I':
            case 'i':
            case 'O':
            case 'o':
            case 'u':
            case 'U':
                printf("it is a vowel");
                break;
            default:
                printf("It is a consonent");
                break;
            }
    }
    else
        printf("it is not an alphabet");
    return 0;
}

Output:

case 1:

Enter any character e
it is an alphabet
it is a vowel

case 2:

Enter any character w
it is an alphabet
It is a consonent

case 3:

Enter any character 5
it is not an alphabet

Question: 3

Write a C program to take input for 2 numbers and an operator (+,-,*,/). Based on the entered operator calculate and display the result.

Sol:

#include <stdio.h>
int main()
{
    float a,b,c;
    char op;
    printf("Enter 2 nos ");
    scanf("%f %f",&a,&b);
    printf("Enter the operator (+, -, *, /)");
    fflush(stdin);
    scanf("%c",&op);
    switch(op)
    {
    case '+':
        c=a+b;
        printf("Sum = %f",c);
        break;
    case '-':
        if(a>b)
            c=a-b;
        else
            c=b-a;
        printf("Diff = %f",c);
        break;
    case '*':
        c=a*b;
        printf("Prod = %f",c);
        break;
    case '/':
        c=a/b;
        printf("div = %f",c);
        break;
    default:
        printf("Invalid operator");
        break;
    }

    return 0;
}

Output:

case 1:

Enter 2 nos 10
20
Enter the operator (+, -, *, /)+
Sum = 30.000000

case 2:

Enter 2 nos 2
3
Enter the operator (+, -, *, /)*
Prod = 6.000000

case 3:

Enter 2 nos 2
6
Enter the operator (+, -, *, /))
Invalid operator

Question: 4

Write a C program to take input for day number and print the day.
Example:
If input is output
1 sun
2 mon
3 tue

7 sat

Sol:

#include <stdio.h>

int main()
{
    int d;
    printf("Enter day number (1..7) ");
    scanf("%d",&d);
    switch(d)
    {
    case 1:
        printf("Sun");
        break;
    case 2:
        printf("Mon");
        break;
    case 3:
        printf("Tue");
        break;
    case 4:
        printf("Wed");
        break;
    case 5:
        printf("Thru");
        break;
    case 6:
        printf("Fri");
        break;
    case 7:
        printf("Sat");
        break;
    default:
        printf("Invalid day number");
        break;
    }
	return 0;
}

Output:

Enter day number (1..7) 2
Mon

Enter day number (1..7) 1
Sun

C Language Programming Tutorial

C Language Tutorial Home     Introduction to C Language     Tokens     If Condition      goto statement and Labelname     Switch Statements     For loop     While Loop     Do while loop     break and continue     Functions     Recursion     Inbuild Functions     Storage Classes     Preprocessor     Arrays     Pointers     Structures and Unions     File Handling     Projects