Calculator using Switch and Conditional Statements

Submitted by: 
Language: 

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.

  1. #include<stdio.h>
  2. #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.

  1. int main(){
  2. int num1,num2,ans = 0;
  3. 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

  1. printf("Enter number 1 :");
  2. scanf("%d",&num1);
  3.  
  4. printf("Enter sign :");
  5. scanf(" %c",&sign);
  6.  
  7. printf("Enter number 2 :");
  8. 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)

  1. if ( sign == '+')
  2. ans = num1 + num2;
  3. else if ( sign == '-')
  4. ans = num1 - num2;
  5. else if ( sign == '*')
  6. ans = num1 * num2;
  7. else if ( sign == '/')
  8. ans = num1 / num2;
  9. else
  10. printf("Error");
  11. printf("Ans is : %d",ans);
  12. }//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
outputCalculator 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.

  1. #include<stdio.h>
  2. #include<conio.h>
  3.  
  4. int main(){
  5. int num1,num2,ans = 0;
  6. char sign;

Simply writing the headers and declaring the variables

  1. printf("Enter number 1 :");
  2. scanf("%d",&num1);
  3. printf("Enter sign :");
  4. scanf(" %c",&sign);
  5. printf("Enter number 2 :");
  6. 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.)

  1. switch ( sign){
  2. case '+':
  3. ans = num1 + num2;
  4. break;
  5. case '-':
  6. ans = num1 - num2;
  7. break;
  8. case '*':
  9. ans = num1 * num2;
  10. break;
  11. case '/':
  12. ans = num1 / num2;
  13. break;
  14. default:
  15. printf("Error");
  16. }
  17. printf("Ans is : %d",ans);
  18. }//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:
output


Comments

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

I am sorry if it's not clear but I am glad you get the idea

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.