Switch Case

Switch Case Example for Simple Arithmetic Operations


The source code illustrating the use Switch Case Example for Simple Arithmetic Operations - Addition, Subtraction, Multiplication and Division are given on this page.

Source Code


#include <stdio.h>
 
 
void main()
{
      int opcode;
      int a, b;
      int result;
 
      printf("Program for Addition, Subtraction, Multiplication and Division\n");
      printf("Enter Your Choice: 1 - Add, 2 - Sub, 3 - Mul, 4 - Div: ");
      scanf("%d", &opcode);
      printf("Enter First Number:");
      scanf("%d", &a);
      printf("Enter Second Number:");
      scanf("%d", &b);
 
      switch(opcode)
      {
      case 1:
            result = a + b;
            printf("%d + %d = %d", a, b, result);
            break;
      case 2:
            result = a - b;
            printf("%d - %d = %d", a, b, result);
            break;
      case 3:
            result = a * b;
            printf("%d * %d = %d", a, b, result);
            break;
      case 4:
            result = a / b;
            printf("%d / %d = %d\n%d %% %d = %d", a, b, result, a, b, a % b);
            break;
      }
}

Output


Program for Addition, Subtraction, Multiplication and Division
Enter Your Choice: 1 - Add, 2 - Sub, 3 - Mul, 4 - Div: 1
Enter First Number: 5
Enter Second Number: 3

5 + 3 = 8

Program for Addition, Subtraction, Multiplication and Division
Enter Your Choice: 1 - Add, 2 - Sub, 3 - Mul, 4 - Div: 2
Enter First Number: 5
Enter Second Number: 3

5 - 3 = 2

Program for Addition, Subtraction, Multiplication and Division
Enter Your Choice: 1 - Add, 2 - Sub, 3 - Mul, 4 - Div: 3
Enter First Number: 5
Enter Second Number: 3

5 * 3 = 15

Program for Addition, Subtraction, Multiplication and Division
Enter Your Choice: 1 - Add, 2 - Sub, 3 - Mul, 4 - Div: 4
Enter First Number: 5
Enter Second Number: 3
5 / 3 = 1
5 % 3 = 2

৩টি মন্তব্য:

  1. #include
    main()
    {
    int a,b,add,sub,div,mul,operation;
    printf(" Enter value of A : ");
    scanf("%d",&a);
    printf(" Enter value of B : ");
    scanf("%d",&b);
    add=a+b;
    sub=a-b;
    mul=a*b;
    div=a/b;
    switch(operation)
    {
    case 1:
    printf("%d",add);
    break;

    case 2:
    printf("%d",sub);
    break;

    case 3:
    printf("%d",div);
    break;

    case 4:
    printf("%d",mul);
    break;
    }
    }

    উত্তরমুছুন
  2. #include
    void main()
    {
    int a,b,c,result=0;
    printf("Enter 1 for Add, 2 for Sub, 3 for Mul, 4 for Div\n");
    scanf("%d", &c);
    printf("Enter the value of A:");
    scanf("%d", &a);
    printf("Enter the value of B:");
    scanf("%d", &b);
    switch(c)
    {
    case 1:
    result = a + b;
    printf("%d + %d = %d", a, b, result);
    break;
    case 2:
    result = a - b;
    printf("%d - %d = %d", a, b, result);
    break;
    case 3:
    result = a * b;
    printf("%d * %d = %d", a, b, result);
    break;
    case 4:
    result = a / b;
    printf("%d / %d = %d\n%d %% %d = %d", a, b, result, a, b, a % b);
    break;
    }
    }

    উত্তরমুছুন