C Online Compiler
Example: C program to make a simple calculator using functions
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// C program to make a simple calculator using functions #include <stdio.h> #include <limits.h> // Addition operation double addition(double operand1, double operand2) { return operand1 + operand2; } // Subtraction operation double subtraction(double operand1, double operand2) { return operand1 - operand2; } // Multiplication operation double multiplication(double operand1, double operand2) { return operand1 * operand2; } // Division operation double division(double operand1, double operand2) { // Check for division by zero if (operand2 != 0) { return operand1 / operand2; } else { printf("\nError! Division by zero.\n"); return INT_MIN; } } int main() { char operator; double operand1 = 0, operand2 = 0; double result = 0; // INPUT the operator printf("Enter an operator from these - (+, -, *, /): "); scanf("%c", &operator); // INPUT the two values printf("\nEnter the two values to make calculation\n"); scanf("%lf %lf", &operand1, &operand2); if (operator== '+') { result = addition(operand1, operand2); } else if (operator== '-') { result = subtraction(operand1, operand2); } else if (operator== '*') { result = multiplication(operand1, operand2); } else if (operator== '/') { result = division(operand1, operand2); } else { printf("\nError! Operator is not correct.\n"); result = INT_MIN; } printf("\nResult: %.2lf\n", result); return 0; }
* 25 60
Output
Clear
ADVERTISEMENTS