Switch Case Program In C With Example Output

Switch Case Program In C With Example – If you are looking for a switch case program in C with an example, this switch case statement tutorial will help you to learn how to write switch case program in C language. Just go through this C program for switch case statement. After going through this C programming tutorial, you will be able to write a switch case program in C.

Switch Case Program in C – Table of Contents

Switch Case Statement

What is switch case statement? A switch case statement is used to test a variable against a list of values. In switch case, each value is called as a case, and the entered variable is called switch. The switched variable is checked against each case value in switch case statement.

Switch Case Statement Syntax in C Programming

Here, you will get Switch Case Syntax in C Programming. This below syntax illustrates how to use switch case statement in C programming to perform arithmetic operations.

//Switch Case Statement Example Syntax in C Programming

switch (n)
​{
 case constant1:
 // logic will be executed if n is equal to constant1;
 break;

case constant2:
 // logic will be executed if n is equal to constant2;
 break;
 .
 .
 .
 default:
 // logic will be executed if n doesn't match any constant
}

C Programming Tutorials

  1. C Program For Palindrome String
  2. C Program For Palindrome Numbers
  3. C Program To Reverse a String with Using Function
  4. C Program To Reverse a String without Using Function
  5. C Program To Reverse a String Using Recursion

Switch Case Program in C

Learn how to write a  Switch case program in C language. Writing a C program for switch case statement can be done using various techniques, but here in this program, we show how to write switch case program in a proper way.

This is the tenth C programming example in the series, it helps newbies, students and B.Tech graduates in enhancing C programming and land on their dream job. All the best guys in learning c programming with coding compiler website. Happy Learning.

Switch Case Program in C Source Code

Copy paste the below switch case program source code or write your own logic into C compilers and run the program to see the result.

/* C PROGRAM FOR SWITCH CASE STATEMENT - SWITCHCASE.C */

#include<stdio.h>
#include<conio.h>
void main()
{
    //variable declaration
    int n1, n2, ch;
    clrscr();
   
    //Asking user to enter a number 
    printf("Enter the first number : ") ;

    //Taking input from user
    scanf("%d", &n1) ;

    //Asking user to enter secpmd number 
    printf("\nEnter the second number : ") ;

    //Taking input from user
    scanf("%d", &n2) ;
    printf("\n[1] -> Addition ") ;
    printf("\n[2] -> Subtraction ") ;
    printf("\n[3] -> Multiplication ") ;
    printf("\n[4] -> Division ") ;

    //Asking your to enter their choice to perform arithmetic operation
    printf("\n\nEnter your choice <1...4> : ") ;
    scanf("%d", &ch) ;

    //Checking user choice against different options in switch case
    switch(ch)
    {
        case 1 :
        printf("\n%d + %d = %d", n1, n2, n1 + n2) ;
        break ;
        case 2 :
        printf("\n%d - %d = %d", n1, n2, n1 - n2) ;
        break ;
        case 3 :
        printf("\n%d * %d = %d", n1, n2, n1 * n2);
        break ;
        case 4 :
        printf("\n%d / %d = %.2f", n1, n2, (float)n1 / n2);
        break ;
        default :
        printf("\nInvalid choice");
        break ;
    }
    getch();
}

Switch Case Program in C OutPut

After you compile and run the above switch case program in C, your C compiler asks you to enter a choice to perform some action using your choice. After you enter your choice, the program will be executed and give output like below example output.

Output – 1

Enter the first number : 11
Enter the second number : 22
[1] -> Addition
[2] -> Subtraction
[3] -> Multiplication
[4] -> Division
Enter your choice <1…4> : 1
11 + 22 = 33

Output – 2

Enter the first number : 15
Enter the second number : 5
[1] -> Addition
[2] -> Subtraction
[3] -> Multiplication
[4] -> Division
Enter your choice <1…4> : 3
15 + 5 = 75

C Programming Examples

  1. C Program To Reverse a String Using Pointers
  2. C Program To Swap Two Numbers Using Two Variables
  3. C Program To Swap Two Numbers Using Three Variables
  4. C Program For Prime Numbers – Check  a Number is Prime or Not
  5. C Program To Reverse a String with Using Function
  6. C Program to Reverse a String without Using Function
  7. C Program to Reverse a Sting Using Recursion

Leave a Reply

Your email address will not be published. Required fields are marked *