This tutorial takes you through everything you need to know to create a simple four function calculator capable of two number addition, subtraction, division or multiplication. Understanding these main concepts is absolutely crucial for getting started with C# and this tutorial is a great starter project for understanding how main functions of C# and Visual Studio Work!
C# (C-Sharp) is an extremely versatile programming language and is used as the driving code in a large number of applications including Unity for game design!
This is the third C# tutorial on this channel and I will start mixing these in with the other software tutorials we do going forward!
If you did enjoy this content or found it useful be sure to leave a like and subscribe to channel for more great content like this, and be sure to check out the channels for hundreds of useful tutorials!
Thanks for watching and good luck with your code!
what's going on guys welcome back to the
channel and today's video is going to be
how to build a four function calculator
using c-sharp in microsoft visual studio
[Music]
so if you're following along with the
c-sharp tutorial series this is episode
three and if you need some introduction
into basic variable types data types and
understanding kind of how to get c-sharp
how to install it and what it's for i
suggest you check out the first two
tutorials on the video on the channel
but if you're just here for the concepts
in this video then that's great let's
get right into it
so
basics of a four function calculator are
we're just gonna do addition subtraction
multiplication and division and we're
not going to worry about building a
graphical user interface for it yet
we're going to handle it all inside the
console
so basically to start let's just start
building out the calculator so the first
thing we're going to want to do is we're
going to want to prompt the user for
two numbers and an operator so we'll say
enter your first number
and i'm just going to kind of write the
prompts first so enter your first number
enter your second number and then we'll
say operator and if you want you could
put the operator
right in between the two numbers
but i'm going to do it this way and then
just to let the user know what we can
expect from it i'm going to add these
parentheses and i'm going to say plus
minus
division or
multiplication okay so i'm going to kind
of add those in brackets just in
parentheses so they know what we're
expecting
now the next thing we want to do is
console.readline
and basically these are how you display
things to the console window and pull
things back from the console window
if you did miss the first two
episodes then that this is something we
covered a lot you just need to know this
is how you get stuff back from the
operators and how you display things to
the console window if you don't have a
built-in graphical user interface
okay so uh for readline it's just that
but we need to store that info somewhere
so we'll call this variable first number
and
we are going to make this the data type
double because we'd like our calculator
to work not just for whole numbers we'd
like it to work for decimals as well we
discussed the differences between
decimal float
integer and double in previous videos so
if you need some background on that
go check that out but the first number
is going to be console.readline but
you're seeing the red squiggly line most
likely if you're using visual studio or
another modern ide
and that's because uh
read lines are always coming in as
strings so if we want this to be a value
of
a decimal value we need to do convert
dot and then two double okay and it's
going to take the value that comes in
and it's just going to turn it into a
double so that's super useful
we are going to do the exact same thing
for second number and we are just going
to change the variable name so second
number is convert to double
and then we actually don't need a
convert for the operator
because we want the operator to stay a
string
all right so let's go ahead and do
operator
and this is instead of double going to
be string
and let's see if i run this let's see
string
opera oh it's operator is a is a
stored procedure for um c sharp so call
it my operator or math operator
something like that there are certain
words you're not going to be able to use
like you can't call a string string
because um that's a built-in so we'll
say my operator
and i'm going to go ahead and run this
just to show you so enter your first
number let's say 12 enter your second
number 23 and i'm going to say enter
plus so should be 35 it's not going to
do anything yet but it's also not giving
us an error that just means everything
is working appropriately
so what we're actually going to do here
is we're going to start processing that
information so we're going to say if
my operator and after the if
you do open and close parentheses and
this is where you put your conditional
statement
so if my operator
is equal to and then for checking is
equal to it's two equal signs right next
to each other just like that
and then we'll start with addition so if
is equal to and then parentheses and
then what i like to do for if statements
is i like to use these curly brackets um
technically if you have one statement
just one if statement followed by one
condition you don't
need it but these curly brackets are
really the most useful way to identify
something is inside of another packet so
inside of an if statement inside of a
function anything like that and all we
have to do if it's addition is we'll go
ahead and grab a console.writeline
and we're going to do do do do
first number
plus
second number and because we're dealing
with numerical values the code is smart
enough to see this addition and instead
of like with two strings it would be
concatenating them and you would just
see like if it was 12 and 23 and they
were strings and then you add them
together it would say one two two three
it would just smash the actual
characters together
because we're using doubles
it's going to perform addition on here
so that's really useful
alright and then we will go ahead and do
this just four times so for if
statements
if the operator is equal to
subtraction if the operator is equal to
division if the operator is equal to
multiplication and all we have to do in
between the two
is update the character and those are
all mathematical characters that c-sharp
visual studio will recognize and perform
the mathematical operation
okay so that should be everything we
need if we go ahead and run this enter
your first number let's say 19 and enter
your second number let's make it tricky
let's do like 10.45
and let's do division
okay 1.8181818
um great let's run it again
let's do something else this time let's
do like subtraction so enter your first
number two
second number 900 and let's do
subtraction
negative 898.
so right there um you know basically 15
maybe 20
lines of code and we've learned how to
uh interpret
mathematical operations from user inputs
and how to process four different
operations just based on a user input so
let's say we want to do one more thing
we want to add a check after the
operator is initially entered and we
want to use some string methods to make
sure that it was valid so let's add
another if statement and say if
my operator oh it needs to be after
order matters so here we go
if
my operator make sure you get your
parentheses
is uh and now we'll use the dot length
function so we talked about this a
little bit in the last video um but one
of the methods you can use on strings is
check how long it is and obviously if
we're checking for an operator um
character we want to make sure the
length is not less than
it is not greater than one so if
operator
length is greater than one then what we
want to do i'll add some curly brackets
we want to basically ask them again to
enter a valid operator so let me go
ahead and copy both of these lines
and we're going to change the text a
little bit so we will say
and you uh when you overwrite a variable
that you've already um identified
previously
you don't need to include the string
again you actually can't include the
string again so what we'll say is enter
instead of your we'll say enter a valid
operator
just basically giving them another
chance to get it right so let's try that
and see
enter your first number
12.3 and then second number
23.5 and we'll say you hit plus but then
you accidentally type in equals as well
well now it's saying okay enter a valid
operator please dummy and then we'll use
a plus
and it tells us 35.8 so that's a cool
way to use a string method to just check
and make sure that you kind of got the
correct spacing
or the correct number of characters
okay so i think this is a super useful
practical c sharp
tutorial how to gather inputs from a
user how to process them and output them
to the screen we did some numerical
manipulation we did some string
manipulation and we used a couple of
different variable types so hopefully
you're finding this c-sharp tutorial
series useful if you have any questions
or you want to see something specific in
a future video be sure to let me know
about in the comments below if you did
find this or anything else on the
channel useful please consider leaving a
like subscribing to the channel it helps
me out a ton and as always good luck
with your code and thanks for watching
thanks bye