Switch case programs using C

In this post you will learn switch case programs using C programming language.The control statement that allows us to make a decision from the number of choices is called a  switch statement.

C program to find day using switch case

#include<stdio.h>
#include<conio.h>
void mian()
{
int day;
clrscr();
printf("enter day number:");
scanf("%d",&day);
switch(day)
{
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 day");
             
}
Output:
Enter day number: 4
Thursday

Example 2:

#include<stdio.h>
void main( )
{
    int a, b, c, choice;
    while(choice != 3)
    {
        /* Printing the available options */
        printf("\n 1. Press 1 for addition");
        printf("\n 2. Press 2 for subtraction");
        printf("\n Enter your choice");
        /* Taking users input */
        scanf("%d", &choice);
     
        switch(choice)
        {
            case 1:
                printf("Enter two numbers");
                scanf("%d%d", &a, &b);
                c = a + b;
                printf("%d", c);
                break;
            case 2:
                printf("Enter two numbers");
                scanf("%d%d", &a, &b);
                c = a - b;
                printf("%d", c);
                break;
            default:
                printf("you have passed a wrong key");
                printf("\n press any key to continue");
        }
    }
}
Output:
Enter your choice: 1
Enter two numbers 10 20
30


Example 3:

int main()
{
int i;
printf("enter an integer");
scanf("%d",&i);
switch(i)
{
case 1: printf("one");
            break;
case 2: printf("two");
            break;
case 3: printf("three");
            break;
case 4: printf("four");
             break;
default: printf("invalid number");
}
return 0;
}

Output:
Enter an integer: 3
three






No comments:

Post a Comment

Basics of CPP Programs

In this post you will learn fundamental C++ programs. These programs for freshers or beginners. 1 .write a cpp program to display hello w...