# Calculator using Switch and Conditional Statements

Calculator using Switch and Conditional Statements In this part you will learn: 1. To make a calculator using Conditional statements 2. What is switch statement 3. To make a calculator using switch statment 4. C syntax 5. Showing output In this tutorial I will teach you how to make a calculator using two different ways, firstly using the Conditional statement method and then using switch statement. Using Conditional statments: Basic Step: Open Dev C++ then File > new > source file and start writing the code below.
`#include<stdio.h>#include<conio.h>`
These are the header files that we need for coding. The main header is stdio.h which provides us with most of the functions for coding in C and the second header files is just for using a function which can pause the screen until the user hits any key.
`int main(){	int num1,num2,ans = 0;	char sign;`
First of all we will declare 3 integers named as ‘num1’,’num2’ and ’ans’, here we have declared 3 integers because in a calculator we will need two integer type variables on which a specific operation will be performed and the result after the operation will be stored in the third integer type variable. Secondly we need we have declared a character named ‘sign’ in which we will stored the specific operation that is needed to be performed. In this program we will only use these variables to take input and storing result as well. In the end the result after arithmetic operation will be displayed on the console
`printf("Enter number 1 :");	scanf("%d",&num1); 	printf("Enter sign :");	scanf(" %c",&sign); 	printf("Enter number 2 :");	scanf("%d",&num2);`
Now we are taking inputs from the user, firstly the user will enter the first number and then second number followed by the operation that the user wants to be performed on these recently entered numbers. Later on after the operation the result will be stored in the integer type variable ‘ans’.(which will be shown later in the code)
`if ( sign == '+')ans = num1 + num2;else if ( sign == '-')ans = num1 - num2;else if ( sign == '*')ans = num1 * num2;else if ( sign == '/')ans = num1 / num2;else printf("Error");printf("Ans is : %d",ans);}//end main`
In these lines of code there are four conditions, if the character given by the user as operation is +,-,/,* the corresponding result will be produced , if these operations are not given ,words ‘Error’ will be displayed in output. Basically what happening in this code is that the compiler checks out that what character is inserted by the user and as referred to my fourth tutorial if a certain conditional statement is satisfied, then a specific operation is performed, for example the user inputs ‘/’ character and compiler comes to first statement that will work only if the character inserted is ‘+’ so it will pass on to the second statement which is ( sign == '-'), so as clearly shown that character inserted by user is ‘/’ so no operation will be performed, sequentially after passing through series of statement , compiler finds if ( sign == '/') statement and the division operation will be performed on the two integers which will be stored in another integer that is ans. There is also a possibility that the characters inserted by the user are not a proper arithmetic operation like addition, subtraction etc so the statement printf("Error") is written in the program to encounter such exceptional cases. In the last line of the program the value contained in ‘ans’ integer is displayed using printf statement . Execute > compile then Execute > run Output Calculator using Switch Statements File > new source file What is switch statement: A switch statement is a statement in which we declare different statements according to which we jump to a specific set of statements performed if the user inputs are matched to the statement of the code . While using switch statements we use a keyword ‘case’ just like ‘if ’ which ensures that right set of statements are executed on a specific input, it will be become more clear as we proceed. Calculator using Switch statement: In this program just like before we will make three integer type variables and one character type variable. Two integer type variables and one character type input is taken from the user and the result is stored in the third integer.
`#include<stdio.h>#include<conio.h> int main(){int num1,num2,ans = 0;char sign;`
Simply writing the headers and declaring the variables
`printf("Enter number 1 :");scanf("%d",&num1);printf("Enter sign :");scanf(" %c",&sign);printf("Enter number 2 :");scanf("%d",&num2);`
Writing the print and scanf statements and getting the sign from the user. (REMEMBER when using %c in scanf put a space befor %c because C language has this problem that it skips white spaces. If you don’t put a space manually it will not prompt you to enter the sign.)
`switch ( sign){case '+':ans = num1 + num2;break;case '-':ans = num1 - num2;break;case '*':ans = num1 * num2;break;case '/':ans = num1 / num2;break;default:printf("Error");}printf("Ans is : %d",ans);getch();}//end main`
Now the most important part is writing the switch statement. In the switch statement we make cases ( you can say possibilities ). In this program we want the program to switch to the statement according the character given +,*,/ and – so we simply pass the character variable to switch statement and make the cases according to the required characters and we break after every statement so that switch ends after executing a single statement and the program gets out of switch brackets, continuing the sequential flow. In case of any other variable is given input we switch to default case which works the same like “else condition”. Here we have added the default at the bottom so we don’t need to write any break statement. After that most our answer will be calculated if the operator is valid. Otherwise it will print error and 0 as ans. After that we write getch for pause and end the main. Execute > compile then Execute > run Output:

Submitted byranie.azote (not verified)on Thu, 12/10/2015 - 15:08

### it is not helped me but i get

it is not helped me but i get the idea of thi kind of programming it is actually completed my idea

### I am sorry

I am sorry if it's not clear but I am glad you get the idea
Submitted byAnonymous (not verified)on Tue, 02/16/2021 - 02:05

### math

A rectangle has two pairs of parallel sides.
Submitted byCristine Joy S… (not verified)on Thu, 12/15/2022 - 08:42

THANKS