TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Simple Calculator Using Switch Statement
Shobana J
Jul 20, 2016
32.8
k
0
3
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
In this blog, you will learn how to create a simple calculator, using switch statement.
calc.zip
Introduction
In this blog, I am going to explain how to do simple calculations by using a calculator.
Software Requirements
Turbo C++ or C.
Programming
#include < stdio.h >
int
main()
{
char
operator;
double
firstNumber, secondNumber;
printf(
"Enter an operator (+, -, *,): "
);
scanf(
"%c"
, & operator);
printf(
"Enter two operands: "
);
scanf(
"%lf %lf"
, & firstNumber, & secondNumber);
switch
(operator)
{
case
'+'
:
printf(
"%.1lf + %.1lf = %.1lf"
, firstNumber, secondNumber, firstNumber + secondNumber);
break
;
case
'-'
:
printf(
"%.1lf - %.1lf = %.1lf"
, firstNumber, secondNumber, firstNumber - secondNumber);
break
;
case
'*'
:
printf(
"%.1lf * %.1lf = %.1lf"
, firstNumber, secondNumber, firstNumber * secondNumber);
break
;
case
'/'
:
printf(
"%.1lf / %.1lf = %.1lf"
, firstNumber, secondNumber, firstNumber / firstNumber);
break
;
default
:
printf(
"Error! operator is not correct"
);
}
return
0;
}
Explanation
By the programming given above, the calculations of addition, subtraction, multiplication and division can be done and explained in a simple manner.
Output
Conclusion:
Thus the simple calculator can get created successfully.
Simple Calculator
Switch Statement
C
Next Recommended Reading
Concatenate Two Strings Using Binary Operator Overloading