Mathmatics in Programming
Some programs you may make will need math problems in it, but, how do you do this? What am I able to do? Are there other mathmatical things I need to know?
For this tutorial you don't need to be some math wizard or expert, its simple 6th grade math for some, and beyound simple equations you aren't taught in school. First though, I suggest you have an understanding of the order of operations (PEMDAS: Perenthesis, exponent, multiply, divide, add, subtract).
Second, lets learn the mathmatical symbols:
- * : The astrik sign, this is multiploication.
- / : backslash, the divideing symbol.
- + : addition
- - : subtraction
- ^ : exponent
Those are ones you are taught in school, however there is two more you should know:
- Mod : This shows the remainder
- \ : This is NOT a division sign! When you divide a number, using this symbol will show only the whole number.
Lets have some examples to understand this new math.
10 Mod 3 = 1
15 Mod 7 = 1
14 Mod 7 = 0
22 Mod 13 = 9
Do you see what its doing yet? Lets look at the first equation. 3 goes into 10 a total of 3 times, 3*3 is only 9, 10-9 = 1. 7 goes into 15 two times, but 7 + 7 is 14, 15-14 = 1. 7 goes into 14 evenly, therefore there is no remainder and 13 only goes into 22 once, so 22-13=9. Now we can move onto '\'
45 \ 7 = 6
10 \ 3 = 3
10 \ 9 = 1
76 \ 34 = 2
This one gives you the whole number. Lets look at them more closely though. 45/7 is 6.428... however it only gave you the 6 leaving the decimals out. The same is true for 10/3 = 3.333... only giving you the 3, 10/9 is 1.1111....leaving the 1. And 76/34 = 2.2352.... giving you only the 2.
Now that we know our mathmatical operations, lets try and make a program that will give us the answers to mathmatical equations. File - >New project. You can name yours what you want. I will make a program which will tell me what 6-5 is. I Create a command button and double click on the button. I will also create a lable and clear it so its blank and name it lblAnswer
When I click on the button I want it to tell me the answer to the equation inside this lable. Here is what my code will look:
lblAnswer.Text = 5 - 6
Thats all there is for it. Now I want it to tell me the answer of the equation six times seven divived by six minus five. How would I write this though? Here is where the order of operations is handy.
lblAnswer = (6*7) / (6-5)
We want it to divide the answers we get from 6*7 and 6-5, so we have to put these in parenthesis so it knows to do this first.
Now lets say we want to find something to user puts in, we'll say we want to find out the taxes for something. Taxes are 8% (at least in this example). I will write the code and then explain it.
Dim Tax, Price As Integer
Price = Val(txtPrice.Text)
Tax = .08
lblPrice.Text = (Price * Tax) + Price
What is this Val(txtPrice.Text) thing? Well, anything put into a text box is automaticly a sting (or words) if we want it to be numbers we put in Val infront of it. In this program I had one button (which is where you add the code), a lable named lblPrice and a textbox named txtPrice.
Integers are numbers, there for instead of having to write txtPrice or .08 all the time I could just write one word. (Price * Tax) is taking whatever the user inputs as the price and muliplying it by 0.08 and then + Price is adding the price they put in along with the result of (Price * Tax).
Thats basicly all there is to know about math really, you can create calculators and converters etc...for more practice you can go to the Mathmatical Project Practice.