» » Switch case in c example program

  • switch case in c programming questions
  • switch case with do you want to continue in c
  • C has a built-in multi-way decision statement known aas a switch.It is a keyword we can select a single option from multiple options.The switch  is having expression;each and every option is known as case and must be followed by colon.
  • Each and every case ends with break statement.
  • The last case is default even that will have break.
  •  The break is a keyword.Each and every case ends with break statement

 SYNTAX:-

 switch(expression)
 {
    case value-1 :block-1 break;
    case value-2 :block-2 break;
    case value-3 :block-3 break;
    .........
    .........
    .........
    .........
    default :default-block break;
 }

#1; switch case in c programming questions

  1. # include <stdio.h>
  2. void main() {
  3.     char operator;
  4.     double a,b;
  5.     printf("Enter an operator (+, -, *,): ");
  6.     scanf("%c", &operator);
  7.     printf("Enter two numbers ");
  8.     scanf("%lf %lf",&a, &b);
  9.     switch(operator)
  10.     {
  11.         case '+':
  12.             printf("%.1lf + %.1lf = %.1lf",a, b, a + b);
  13.             break;
  14.         case '-':
  15.             printf("%.1lf - %.1lf = %.1lf",a, b, a - b);
  16.             break;
  17.         case '*':
  18.             printf("%.1lf * %.1lf = %.1lf",a, b, a * b);
  19.             break;
  20.         case '/':
  21.             printf("%.1lf / %.1lf = %.1lf",a, b, a / b);
  22.             break;
  23.         default:
  24.             printf("Error! operator is not correct");
  25.     }
  26. }

Output:
  1. Enter an operator (+, -, *,): +
  2. Enter two numbers 12
  3. 12
  4. 12.0 + 12.0 = 24.0
  5. Process returned 18 (0x12)   execution time : 7.392 s
  6. Press any key to continue.





Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu