Hello and welcome back to this practical module and in this video, we will be discussing a new problem statement.
And what is exactly is that problem statement?..... Like we use a calculator in maths.
And in that calculator we know that we only have to write our operands, along with that we have to tell the calculator that which operation we have to perform on them and we just have to select that operation and have to press the equal to sign because of which we get the solution or the answer.
We are going to make this kind of calculator in this video and how will that be made and how we'll have to write its program, which all functionalities we would be using all these things we would be seeing in this video.
So, without any delay, we will start and we will see the C program where we are making a calculator.
Now in this calculator, there will be four functionalities, that would be addition, subtraction, multiplication or division.
All these four functionalities, which are there in any calculator.
Those would also be there in our calculator.
And along with that, the user will have the option, if the user has to multiply between two operands, he can select the multiplication symbol.
Along with that if he wants to do addition, he can get the addition done.
If he wants to subtract, he can also get the subtraction done.
The program is going to be just one but the operations would be different.
You can do different operations over different operands.
So, we will quickly go into our text editor and see how we will be making this program and how it will work efficiently.
So, we will quickly go into our Visual Studio Code.
So here we have come into our Visual Studio Code.
And as we all know before writing any C program, the first step is to write the preprocessing command.
In the same way we will write our preprocessor command over here, which is stdio.h header file include command.
Here we are definitely going to use the printf and scanf function, that's why we have imported the stdio.h header file, the library, we have imported over here.
Now we will go ahead and see which all things we are going to include here.
If I talk about any C program, then the important part that is there in it that is our main function.
Even here we will start our main function.
If before the main function we would be making some other function, if we would be declaring some other function, at that time we will be writing all those things before.
But in this program for now we will not be making one function which means a user defined function.
That's why after the preprocessing command we'll be writing our main function over here.
So, here you can see we have started the main function and in that basically we have made few variables in different data types.
Now what is the work of these variables we will quickly see here, we have made a variable named “op” which is of character data type, in that we will be storing one operator.
And basically, the logic over here is that we will be asking the user for an operator whether you want to perform addition.
If you want to perform addition then the addition operator symbol that will be pressed by the user.
If he wants to do multiplication, then the multiplication symbol will be used.
Along with that if division has to be done then the user will enter slash.
And if the subtraction has to be done then minus sign obviously will be entered by the user.
When the user will enter it, in which variable will it be stored with us? So that we get to know that the user has to do addition or subtraction.
So that will be stored inside the op variable.
So that's why we have kept the data type as character, so that there won't be any problem of any type over here.
Because if we keep integer then obviously a Asterix mark or divisions sign, slash.
That obviously is not a numeric value.
That's why we have not kept the data type as an integer.
We have kept it character because all these are a type of character.
That's why we have named op variable as character and in op, we are going to store those operators which basically helps us to store the operators which basically, helps us to perform the operations.
Now we will go ahead and see our two more variables which we have made here.
The two variables that we have made first and second which are of Double data type.
First and second are those variables in which we will be storing our operands.
So, definitely the input for all these things will be taken from the user.
Because of this the calculator is successful.
The user is giving his value, giving his operator and upon that we are performing the operation and giving the user.
So, this was about our few variables.
Now we will go ahead and see how we are taking inputs in these variables.
So, here first of all, we have asked the user enter an operator.
So, you can see here that we have made use of the printf statement and we have printed one statement on our output screen, which we will go ahead and see how it is getting printed.
So, through printf statement we have asked the user to enter an operator, it can be plus, minus, multiplication operator or division operator.
So, all these operators which are there, from this any one operator would be entered by the user.
So, that through the scanf function, we will be storing in the op variable.
So, this was about the printf and scanf function.
From the user we have taken the operator to us.
Now the time comes of the operands, operator has come to us.
Now we want the operands on which we will be performing the operations, definitely even that we will be taking from our user.
So, we tell our user enter two operands and with the help of printf statement when this statement gets printed on our output screen the user then enters the two numbers, two numeric values or two operands.
And with the help of scanf function and with the help of lf format specifier, we would be storing both those values in our variable.
So, this was about how we have taken operator from our user and how we have also taken our operands from the user.
Now here time comes for the main logic over here, how our calculator will find out, since the user now wants addition, that's why he has passed the addition symbol.
So, how our computer or our program gets to know that we have to perform addition over here.
So, for that, we will be using here, switch case statement and what we will do is we will be making different cases for each operator.
For example, if the user has entered addition operator, what we will do is we will make one addition symbol case, whenever that will be satisfied.
Taking the two operators we perform the addition function and then print the output on our output screen.
In the same way if the user has entered the multiplication symbol in our op variable.
So, in that we will make one case, which means in switch case cases that will make, one case we will make for multiplication.
And there between both the.
operations we perform that multiplication operation and print its output on our output screen.
So somewhat in this video, we'll be using our switch case statement over here.
Quickly we will see how we will make this switch case statement.
So first of all, for the switch case statement we will have to write a key word which itself is switch and after writing switch our compiler gets to know that here we are using switch case statement.
After that we have put that variable in bracket based on which different cases will come.
Yeah, we have put the op variable, inside op if plus comes then the plus case works.
If multiplication comes inside op, then we will run the multiplication case and similarly subtraction and division will also work.
So, here inside the switch statement, the first case that we have written is the case of addition.
So, you can see that after writing the case we have put the plus operator, so this switch case statement in which we all know that in the variable the value that is there.
It is being checked.
It is matched with each and every case.
And in any case, when it matches in that whichever statement has been written, that runs and with the help of break statement we come out of the switch case statement.
In this way the work is going to happen over here.
For now, here we have put case of plus symbol.
So, you can see that in that we have written this printf statement and in the printf statement with the help of some few format specifiers first operant plus second operant is equal to whatever is the addition of that we have printed it over here, as you can see here, we have used the format specifier, “%lf” and along with that we have also written .1.
So, we all know that double comes in the decimal digit and here five to six 0s keeps on adding after the decimal point.
So, if I want to round off that number, only till one digit, which means after decimal I want only one digit in my value.
So, there I will use “%.1lf” format specifier.
If I want two points after my decimal, then I will use %.1lf.
So in this way we can tweak the format specifier as per our use.
If I want to round up the decimal value till two points then I will be using .2, I want to round up till three points, then .3 will be used and in this way, as per requirement we will keep on tweaking it.
So, you'll see that in the printf statement, we will first of all print the first variable, we will print that with the help of this format specifier.
After that our second variable which is our second operand, we will write that.
So, here on the output screen something like this will be printed.
Suppose user has given two and three and he has passed operator plus.
Then in the output screen it will be passed two plus three is equal to whatever is the addition of two plus three.
In this case it will be five, so five will get printed over here on our output screen.
In the same way we will be adding few more cases here, like we have done it for addition, we can also do it for subtraction.
Only the operation will be changed over here.
First + second we had done in the addition case.
Now first - second we'll be doing in subtraction case, in the same way if we have multiplication, for multiplication we will be passing first multiplied by second and we will get printed on our output screen.
Along with that if we want to perform division, then for division we have made one case over here.
So first/ second will get printed on the output screen.
So, this was about our switch case statement.
Now we all know that in the switch case statement we also write one default case.
Even though this is not compulsory, this default case we can even skip.
But suppose if the user passes some other operator, other than these four operators, which do not match with these cases.
In that case, basically, our default statement will run and in the default statement we'll be writing since you have seen that we have earlier passed that addition, subtraction, multiplication or division.
From all these four operators, only one has to be passed.
In that case, if our user wants to pass any operator other than these four operators, then definitely our default statement will be executed over here and it will be printed an error “operator is not correct.” So somehow in this way, our switch case statements will go on.
So, this was about our switch case statement and after that, we will close our main function.
So, what are we doing over here, so we will quickly see from the start.
So, first of all we made three variables.
One variable we made to store the operator.
We made two variables to store the operands.
Now we took operands and operator’s input with the help of printf and scanf.
As soon as we took that input now we know that the operation that has to be done by the user that is stored in our op variable, right? If the user has to do a multiplication.
I will not execute this plus case because the multiplication symbol will not match here with this case.
In the same way it will also not it will not be matched with the subtraction case but it will match with our case multiplication.
And it will execute this case.
It will execute it means that the printf statement that we have written in this that will be executed over here and first multiplied by second’s output will come and after that we will break which means that we will come out of the switch case statement.
So, this was the entire flow of making the calculator with the help of switch case statement.
Apart from switch case you can also use if and else statements.
In the same way our control flow statements are there which you can use over here.
But in this case, we have used a switch case statement because here I found it more effective, that's why we have used it over here.
So now comes the time.
The most important part of this video.
The program that we have made till now, we have written till now basically we have to run it and execute it and see.
So, for that we will come into our terminal and here what we will do is we will tell our compiler that we have to compile this particular file.
So, for compile, we'll have to use the gcc statement.
So, we have written our file’s name “gcc switch calculator.c “, as soon as we've pressed it, we have seen that it has been compiled.
Now after it has been compiled what we have to do? We have to run it.
As soon as I run it you can see then I have got a printf statement printed which is asking me, enter an operator and the operator over here is plus, minus, multiplication or division.
So, suppose if I want to do multiplication, I will pass the multiplication operator over here and press enter.
After that the time comes for writing two operands, suppose we have to multiply four and three.
So, suppose we want to multiply four and three.
So here, I have passed four and three with the help of space for differentiation, for the scanf statement, as soon as I will enter here you can see over here that the commands will run.
As soon as I passed the operands, our switch case statement started running and it checked that our operator is matching with which case, it is not a matching with plus, skip it, it is not matching with minus, skip it, it is matching with the multiplication, execute the statement inside this and the statement inside it, you all know that we had written, what you have to do is you have to do 4X3, whatever operands that we have passed.
We have to multiply them and then the output should be printed over here.
Since we had used “%.1lf”, over here, you can see that the decimal has rounded off here only till the one decimal place.
In this way, 4X3 is equal to 12, with the help of our calculator which we have made here in so little time, so quickly.
We will run it one more time.
And this time I will perform the division operation and see, what I will do here is I will do 10 divided by 2.
And as soon as I do it, you can see here that the output has come here 5, like you use calculators in any mobile or you're using some other application for using the calculator, even there you can make your own C program, where you can perform any calculations with the help of built in C libraries as well as built in logic, which we have used here of the switch case statement.
So, this was about how we have to make a switch calculator.
So, you can also execute it in your computer with which you will get a clear idea, how our switch case statements work.
If you have any questions related to this topic or program.
You can ask those questions definitely by going on forums.learnvern.com and in your language you can ask those questions to us and we will reply you there as soon as possible.
Thank you so much.
Share a personalized message with your friends.