Software & Finance





C Programming (Turbo C++ Compiler) 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