Locked lesson.
About this lesson
Let's learn how to perform basic math functions: Addition, Subtraction, Multiplication, Division.
Exercise files
Download this lesson’s related exercise files.
13 - Math (Addition, Subtraction, Multiplication, Division).docx60.8 KB 13 - Math (Addition, Subtraction, Multiplication, Division) SOLUTION.docx
57.8 KB
Quick reference
Math (Addition, Subtraction, Multiplication, Division)
C# allows us to do math easily.
When to use
Do this whenever you need to use math in your program.
Instructions
Math operators include:
+ // addition
- // subtraction
* // multiplication
/ // division
Hints & tips
- The basic math operators are + - * /
- They act mostly how you would expect them to act
- If you perform math on variables, be aware of the data types you use: int, float, etc.
- 00:04 Okay, in this video, I want to start to talk about basic math for
- 00:07 C# math operators.
- 00:09 And basic math is almost exactly like you would think it would be.
- 00:13 We can use all the operators, the math operators, the arithmetic operators that
- 00:17 you knew growing up in grade school, the plus sign, the minus sign.
- 00:20 Multiplication is the star, which on my keyboard is Shift+8.
- 00:25 Make that and then division is the forward slash.
- 00:27 So let's come through here and let's create some integers.
- 00:30 So I'm going to call this x, and we'll make this 6.
- 00:34 And let's create another one.
- 00:35 We'll call this y, and this will be 3.
- 00:38 And right off the bat, we can do this very easily.
- 00:41 We could say x + y.
- 00:44 Now if we run this, 6 + 3, we expect this to be 9.
- 00:50 And we get 9.
- 00:51 Now this might be a little confusing because we've been using this plus sign to
- 00:54 concatenate things in the past.
- 00:56 And in fact, if we use a string in here, and we say add, and
- 01:01 then try to concatenate again, we're going to get something very different.
- 01:08 We're going to see add 63, but did it do it took 6 and 3 and
- 01:12 just smooshed them together, it didn't actually add.
- 01:16 So if you're going to do any sort of math on a string like this,
- 01:20 we can't do it like that, we have to do it outside of here.
- 01:24 So let's call this answer = x + y.
- 01:28 So, let's also say int, define this as answer.
- 01:35 Now we can take this and bring it in here.
- 01:39 And if we save this and run it, we will give away expect at 9, right?
- 01:45 So just sort of keep that in mind.
- 01:47 If you want to do your math inside of here, there can't be any strings also.
- 01:52 So, okay, that's kind of interesting.
- 01:54 So let's just look at these really quick.
- 01:56 Let's go x- y.
- 01:58 Of course, easily we know this is going to be 6 minus 3, so we should get 3.
- 02:04 Nothing weird there.
- 02:05 What if we do the opposite?
- 02:06 What if we say y- x?
- 02:08 This is going to be 3 minus 6.
- 02:10 This should be negative 3.
- 02:12 And in fact, we see, yep, sure enough negative 3, so
- 02:15 we can have negative answers too, no big deal.
- 02:18 So let's change this back.
- 02:19 So now let's go multiplication, x times y.
- 02:23 So 6 times 3 is 18, we should see.
- 02:28 Sure enough, 18.
- 02:30 And now division is a little bit trickier, right?
- 02:33 So 6 divided by 3, that's no problem, right?
- 02:38 That's 2.
- 02:39 Okay, that makes sense.
- 02:40 What about 3 divided by 6, which is y divided by x?
- 02:48 We save this and run it.
- 02:51 We get 0.
- 02:52 What's going on here?
- 02:53 Well, remember we're dealing with integers here, and integers are whole numbers.
- 02:57 So 3 divided by 6 is 0.5.
- 03:01 C# doesn't know what to do with 0.5 for an integer, so it just returns a 0.
- 03:06 So we could change these to floats, or doubles, or something like that.
- 03:12 If we do that, now we're not going to have any problems.
- 03:16 We'll get our 0.5 that we would expect to get, and that's all there is to it.
- 03:21 So those are our basic math operators, plus, minus, multiplication, and division.
- 03:25 Like I said, they react almost exactly how you would think they would react.
- 03:30 Addition does addition, subtraction does subtraction.
- 03:32 The only weirdness is that division where we have to use floats also,
- 03:39 0 divided by 6, if we run this We get 0, okay, that makes sense.
- 03:47 What about, say y divided by 0?
- 03:50 Well, we know from regular math, that's infinity.
- 03:55 And just so you can see maybe sort of make out the little infinity symbol there.
- 03:59 So as sometimes with calculators, we'll get an error if you try and do that, but
- 04:02 at least it's returning infinity, so that's interesting.
- 04:05 And that's all there is to it.
- 04:06 So that's all for basic math operators.
- 04:09 In the next video, we'll look at exponents and modulus.
Lesson notes are only available for subscribers.