Thank you for printing our content at www.domain-name.com. Please check back soon for new contents.
(https://www.programiz.com/learn-c?
Get
utm_campaign=programiz-
App(/)
Search tutorials and examples
homepage&utm_source=programiz-website-c-app-popup)
www.domain-name.com
A DV E RT I S E M E N T S
C Program to Make a Simple
Calculator Using
switch...case
In this example, you will learn to create a simple
calculator in C programming using the switch statement.
To understand this example, you should have the
knowledge of the following C programming (/c-
programming) topics:
C switch Statement (/c-programming/c-switch-case-
statement)
C break and continue (/c-programming/c-break-
continue-statement)
Thank you for printing our content at www.domain-name.com. Please check back soon for new contents.
This (https://www.programiz.com/learn-c?
program takes an arithmetic operator +, -, *, /
Get
utm_campaign=programiz-
App(/)
Search tutorials and examples
and two operands from the user. Then, it performs the
homepage&utm_source=programiz-website-c-app-popup)
calculation on thewww.domain-name.com
two operands depending upon the
operator entered by the user.
Simple Calculator using switch Statement
#include <stdio.h>
int main() {
char op;
double first, second;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &op);
printf("Enter two operands: ");
scanf("%lf %lf", &first, &second);
switch (op) {
case '+':
printf("%.1lf + %.1lf = %.1lf", first, second, fi
break;
case '-':
printf("%.1lf - %.1lf = %.1lf", first, second, fi
break;
case '*':
printf("%.1lf * %.1lf = %.1lf", first, second, fi
break;
case '/':
printf("%.1lf / %.1lf = %.1lf", first, second, fi
break;
// operator doesn't match any case constant
default:
printf("Error! operator is not correct");
return 0;
Output
Thank you for printing our content at www.domain-name.com. Please check back soon for new contents.
Enter an operator (+, -, *,): *
Enter two operands: 1.5
(https://www.programiz.com/learn-c?
Get
4.5
App
utm_campaign=programiz-
Search tutorials and examples
1.5(/) * 4.5 = 6.8
homepage&utm_source=programiz-website-c-app-popup)
www.domain-name.com
The * operator entered by the user is stored in op . And,
the two operands, 1.5 and 4.5 are stored in first and
second respectively.
Since the operator * matches case '*': , the control of
the program jumps to
printf("%.1lf * %.1lf = %.1lf", first, second, first * se
This statement calculates the product and displays it on
the screen.
To make our output look cleaner, we have simply limited
the output to one decimal place using the code %.1lf .
Finally, the break; statement ends the switch
statement.
Share on:
(https://www.facebook.com/sharer/sharer.php? (https://twitter.com/inte
u=https://www.programiz.com/c- text=Check%20this%20
programming/examples/calculator-switch-case) programming/examples/
Did you find this article helpful?
A DV E RT I S E M E N T S
Thank you for printing our content at www.domain-name.com. Please check back soon for new contents.
(https://www.programiz.com/learn-c?
Get
App utm_campaign=programiz-
Search tutorials and examples
(/)
homepage&utm_source=programiz-website-c-app-popup)
www.domain-name.com
Related Examples
C Example
Print an Integer (Entered by the User)
(/c-programming/examples/print-integer)
C Example
Multiply Two Floating-Point Numbers
(/c-programming/examples/product-numbers)
C Example
Compute Quotient and Remainder
(/c-programming/examples/remainder-quotient)
C Example
Check Whether a Number is Even or Odd
Thank you for printing our content at www.domain-name.com. Please check back soon for new contents.
(https://www.programiz.com/learn-c?
(/c-programming/examples/even-odd)
Get
App utm_campaign=programiz-
Search tutorials and examples
(/)
homepage&utm_source=programiz-website-c-app-popup)
www.domain-name.com