C Programming Code Examples C > If Else and Switch Case Code Examples Program to create calculator using switch...case and functions Program to create calculator using switch...case and functions Step by step descriptive logic to create menu driven calculator that performs all basic arithmetic operations. Input two numbers and a character from user in the given format. Store them in some variable say j1, op and j2. Switch the value of op i.e. switch(op). There are four possible values of op i.e. '+', '-', '*' and '/'. For case '+' perform addition and store result in some variable i.e. result = j1 + j2. Similarly for case '-' perform subtraction and store result in some variable i.e. result = j1 - j2. Repeat the process for multiplication and division. Finally print the value of result. #include <stdio.h> /** Function declarations for calculator */ float add(float j1, float j2); float sub(float j1, float j2); float mult(float j1, float j2); float div(float j1, float j2); int main() { char op; float j1, j2, result=0.0f; /* Print welcome message */ printf("WELCOME TO SIMPLE CALCULATOR\n"); printf("----------------------------\n"); printf("Enter [number 1] [+ - * /] [number 2]\n"); /* Input two number and operator from user */ scanf("%f %c %f", &j1, &op, &j2); switch(op) { case '+': result = add(j1, j2); break; case '-': result = sub(j1, j2); break; case '*': result = mult(j1, j2); break; case '/': result = div(j1, j2); break; default: printf("Invalid operator"); } /* Print the result */ printf("%.2f %c %.2f = %.2f", j1, op, j2, result); return 0; } /** Function to add two numbers */ float add(float j1, float j2) { return j1 + j2; } /** Function to subtract two numbers */ float sub(float j1, float j2) { return j1 - j2; } /** Function to multiply two numbers */ float mult(float j1, float j2) { return j1 * j2; } /** Function to divide two numbers */ float div(float j1, float j2) { return j1 / j2; }