C Tutorial

In c programming, switch case is used to perform the different logic's based on different output values of an expression.

switch case is used to reduce the complexity of using multiple if else statements.

We can have a separate logic for each case in switch, if switch expression output matches any of the case, then particular case logic will be executed. if no case matched, default block will be executed.

switch case syntax ,

switch(expression) { case: // logic 1; break; case : // logic 2; break; ------------ ------------ default: // logic-default }

This c program is used to perform arithmetic calculations based on user input operation.

#include<stdio.h> void main() { int val1, val2; char op; printf("\nCalculator"); printf("\n--------------"); printf("\n + Add\n * Multiplication\n - Subtraction\n / Division\n % Modulus"); printf("\nEnter Operation: "); scanf("%c", &op); printf("\nEnter Value 1: "); scanf("%d", &val1); printf("\nEnter Value 2: "); scanf("%d", &val2); switch(op) { case '+': printf("Addition of %d and %d: %d", val1, val2, val1+val2); break; case '*': printf("Multiplication of %d and %d: %d", val1, val2, val1*val2); break; case '-': printf("Subtraction of %d and %d: %d", val1, val2, val1-val2); break; case '/': printf("Division of %d and %d: %d", val1, val2, val1/val2); break; case '%': printf("Modulo of %d and %d: %d", val1, val2, val1%val2); return; default: printf("\nInvalid operator!"); } }

Output:

$ cc switch.c $ ./a.out Calculator -------------- + Add * Multiplication - Subtraction / Division % Modulus Enter Operation: + Enter Value 1: 23 Enter Value 2: 12 Addition of 23 and 12: 35 $ ./a.out Calculator -------------- + Add * Multiplication - Subtraction / Division % Modulus Enter Operation: * Enter Value 1: 2 Enter Value 2: 6 Multiplication of 2 and 6: 12 $ ./a.out Calculator -------------- + Add * Multiplication - Subtraction / Division % Modulus Enter Operation: - Enter Value 1: 6 Enter Value 2: 3 Subtraction of 6 and 3: 3 $ ./a.out Calculator -------------- + Add * Multiplication - Subtraction / Division % Modulus Enter Operation: / Enter Value 1: 25 Enter Value 2: 5 Division of 25 and 5: 5 $ ./a.out Calculator -------------- + Add * Multiplication - Subtraction / Division % Modulus Enter Operation: % Enter Value 1: 7 Enter Value 2: 4 Modulo of 7 and 4: 3 $ ./a.out Calculator -------------- + Add * Multiplication - Subtraction / Division % Modulus Enter Operation: $ Enter Value 1: e Enter Value 2: Invalid operator!

#include<stdio.h> void main() { int val1, val2; char op; printf("\nCalculator"); printf("\n--------------"); printf("\n + Add\n * Multiplication\n - Subtraction\n / Division\n % Modulus"); printf("\nEnter Operation: "); scanf("%c", &op); printf("\nEnter Value 1: "); scanf("%d", &val1); printf("\nEnter Value 2: "); scanf("%d", &val2); switch(op) { case '+': printf("\nAddition of %d and %d: %d", val1, val2, val1+val2); case '*': printf("\nMultiplication of %d and %d: %d", val1, val2, val1*val2); case '-': printf("\nSubtraction of %d and %d: %d", val1, val2, val1-val2); case '/': printf("\nDivision of %d and %d: %d", val1, val2, val1/val2); case '%': printf("\nModulo of %d and %d: %d", val1, val2, val1%val2); default: printf("\nInvalid operator!"); } }

Output:

$ cc switch.c $ ./a.out Calculator -------------- + Add * Multiplication - Subtraction / Division % Modulus Enter Operation: + Enter Value 1: 5 Enter Value 2: 4 Addition of 5 and 4: 9 Multiplication of 5 and 4: 20 Subtraction of 5 and 4: 1 Division of 5 and 4: 1 Modulo of 5 and 4: 1 Invalid operator! $ ./a.out Calculator -------------- + Add * Multiplication - Subtraction / Division % Modulus Enter Operation: - Enter Value 1: 5 Enter Value 2: 2 Subtraction of 5 and 2: 3 Division of 5 and 2: 2 Modulo of 5 and 2: 1 Invalid operator!

C Program Examples

Privacy Policy | Copyright@2017 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page

^