Locked lesson.
About this lesson
In this lesson, we examine how Exponents work and what the Modulus is.
Exercise files
Download this lesson’s related exercise files.
14 - Math (Exponents and Modulus).docx60.8 KB 14 - Math (Exponents and Modulus) SOLUTION.docx
57.8 KB
Quick reference
Math (Exponents and Modulus)
Exponents allow us to raise numbers to the power of some other number. Modulus returns the remainder when you divide one number by another.
When to use
Use these whenever you need to use an exponent or return a remainder of division.
Instructions
int x = 3;
int y = 2;
Math.Pow(x,y); // exponent
x % y // modulus
Hints & tips
- Math.Pow(x,y); // exponent - x to the power of y
- x % y // modulus returns remainder of division (x divided by y)
- 00:04 Okay, in this video I want to continue talking about math a little bit,
- 00:07 with a little bit more advanced math, not terribly advanced, but
- 00:09 a little bit more advanced than in the last video.
- 00:11 And we want to look at exponents and modulus.
- 00:14 And if you're not familiar with an exponent, that's, to the power of.
- 00:18 So, if you wanted 3 to the power of 3, which is 3 times 3 times 3, right?
- 00:23 Exponent, that's what that is.
- 00:25 Actually, we don't use a caret.
- 00:27 And many other programming languages, and in math, in general,
- 00:30 you'll use a caret to show an exponent.
- 00:32 Not the case with C#, so, we'll talk about that.
- 00:36 But we'll get into that in a second.
- 00:38 A modulus is a remainder.
- 00:39 So, let's say we had 14 divided by 5.
- 00:44 Well, 5 goes into 14, 2 times,
- 00:46 2 times 5 is 10 with 4 leftover, for remainder, right?
- 00:50 Modulus returns that remainder.
- 00:53 So, that's very handy for all kinds of things.
- 00:55 Those are the two things we're going to talk about in this video.
- 00:57 So, let's go again int x = 3 and
- 01:02 int y, I don't know, = 3, again.
- 01:06 I don't want to go crazy on the exponents.
- 01:08 But, normally, for exponents, in a lot of programming languages you would go x and
- 01:14 then double multiplication signs and then y.
- 01:18 Now, if we save this and run it here, this is not going to work.
- 01:21 See, we're going to get errors, and it doesn't return anything.
- 01:25 So, C# is a little bit strange, as far as programing language goes.
- 01:28 It doesn't have an exponent operator, which is very weird, its just strange.
- 01:33 But the .net that C# is build on top of, has a function called a math power.
- 01:37 So, that's what we can use.
- 01:39 And to do that, we just call Math.Pow, right?
- 01:45 And then inside of here, we pass the two things that we want to do.
- 01:49 So, if we wanted x to the power of y, we would do it like that.
- 01:53 So, 3 times 3 times 3, that should be 27.
- 01:55 If we save this and run it, we get an error.
- 01:58 We need an extra bracket there.
- 02:00 There we go.
- 02:02 Now, if we save this and run it, we get 27, right?
- 02:06 So, kind of weird, let me just put this Math.Pow and say first and second.
- 02:12 So, its first would be 3 raised to the power of the second thing,
- 02:18 which here is also 3.
- 02:19 So, if we change this to 4, this would be 3 raised to the power of 4, right?
- 02:24 So, if we run this, 27 times 3 is what?
- 02:28 81, so, there you go.
- 02:30 So, that's exponents.
- 02:32 Pretty easy, modulus is just this % sign, which is a little weird, but
- 02:37 that's common in most programming languages.
- 02:40 Let's take our previous example 14 divided by 5.
- 02:43 So, here, let's go x modulus y.
- 02:48 So like I said, 14 divided by 5 is 2, 2 times 5 is 10.
- 02:53 With 4 leftover, this should return 4, whatever that remainder is,
- 02:57 in this case 4.
- 02:57 So, if we run this, we get, sure enough, 4, and that's what we expect.
- 03:04 A modulus, I find,
- 03:05 a lot of beginner coding students have a hard time wrapping their brain around.
- 03:09 Why would we ever need a remainder?
- 03:11 Believe me, you're going to need to find remainders for
- 03:13 all kinds of different things.
- 03:15 And we might have an example of that later on.
- 03:16 But, very powerful, very easy to use modulus.
- 03:19 And the exponents, it's Math.Pow, which is a little weird, but works just fine.
- 03:24 Why do you need an operator when you could just call the Math.Pow function?
- 03:27 So, that's how they set that up, and that's all there is to it.
- 03:31 So, that's all for modulus and exponents.
- 03:32 In the next video, we'll look at math order of operations.
Lesson notes are only available for subscribers.