C Program to Make a Simple Calculator
C Program to Make a Simple Calculator
A school calculator selects one of four arithmetic operations. It should report an unsupported operation or invalid divisor rather than continue with an uninitialized result.
A switch selects a case for an integral value such as a character; explicit break statements prevent unintended fall-through into another operation.
Before this exercise, review operators and conversions and decisions and conditions. The worked program below applies those ideas to this specific problem.
Explain
Explain why a small calculator uses this sequence of operations.
Trace
Trace the sample values and predict the printed result.
Apply
Choose division with divisor zero and predict the safe outcome.
A Small Calculator: follow the data
Given data
A school calculator selects one of four arithmetic operations. It should report an unsupported operation or invalid divisor rather than continue with an uninitialized result.
Rule
A switch selects a case for an integral value such as a character; explicit break statements prevent unintended fall-through into another operation.
Observed result
Result: 75.0
Understand the operation
The operation character chooses one matching case. Each valid arithmetic case stores result and breaks; division first checks its divisor. The default branch reports an unsupported selection.
Returning early from invalid branches ensures result is never read without a store. This is a small arithmetic dispatcher, not a parser for full mathematical expressions or operator precedence.
Read the complete C program
The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.
C17 · complete program
#include <stdio.h>
int main(void) {
double first = 40.0, second = 35.0, result;
char operation = '+';
switch (operation) {
case '+': result = first + second; break;
case '-': result = first - second; break;
case '*': result = first * second; break;
case '/':
if (second == 0.0) { puts("Cannot divide by zero"); return 0; }
result = first / second; break;
default: puts("Unsupported operation"); return 0;
}
printf("Result: %.1f\n", result);
return 0;
}
Expected output
Result: 75.0
Run in C compiler →Follow the values through the code
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
double first = 40.0, second = 35.0, result;
char operation = '+';
switch (operation) {
case '+': result = first + second; break;
case '-': result = first - second; break;
case '*': result = first * second; break;
case '/':
if (second == 0.0) { puts("Cannot divide by zero"); return 0; }
result = first / second; break;
default: puts("Unsupported operation"); return 0;
}
printf("Result: %.1f\n", result);
return 0;
}
The operation is plus and the operands are 40 and 35.
The plus case stores 75 and breaks; printf then formats the result with one fractional digit.
Follow the worked calculation
Start with the sample
The operation is plus and the operands are 40 and 35.
Apply the rule
The plus case stores 75 and breaks; printf then formats the result with one fractional digit.
Check the result
Result: 75.0
Result: 75.0
Try a small variation
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
double first = 40.0, second = 35.0, result;
char operation = '+';
switch (operation) {
case '+': result = first + second; break;
case '-': result = first - second; break;
case '*': result = first * second; break;
case '/':
if (second == 0.0) { puts("Cannot divide by zero"); return 0; }
result = first / second; break;
default: puts("Unsupported operation"); return 0;
}
printf("Result: %.1f\n", result);
return 0;
}
Choose division with divisor zero and predict the safe outcome.
Worked answer
The division case prints an explanatory message and returns before the division or final result print. It does not invent a numeric answer for the invalid operation.
C17 · complete program
#include <stdio.h>
int main(void) {
double first = 40.0, second = 0.0, result;
char operation = '/';
switch (operation) {
case '+': result = first + second; break;
case '-': result = first - second; break;
case '*': result = first * second; break;
case '/':
if (second == 0.0) { puts("Cannot divide by zero"); return 0; }
result = first / second; break;
default: puts("Unsupported operation"); return 0;
}
printf("Result: %.1f\n", result);
return 0;
}
Expected output
Cannot divide by zero
Run in C compiler →Recognize the important boundaries
Floating-point nonfinite values need an application policy in a general calculator; this demonstration’s operands are finite literal fixtures.
Without a break or early return, control continues into following case statements. Use fall-through only when it is deliberate and explained.
Check your understanding
Choose the answer, then compare the reasoning for every option.
Use the idea independently
A switch selects a case for an integral value such as a character; explicit break statements prevent unintended fall-through into another operation.
Continue with validity checks before computation.
Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.
Reaching the recap and completing the quizzes are separate milestones.