How to do Math - Programming Style

Doing math with a computer program is incredibly easy. Take a look at this Console program as an example:



In case you haven't already guessed, when you run this program, it outputs the answer "9." Simple, huh!? Now there are a few tricks to doing math with a computer program.

Integers: If you write your answer as an integer, it will convert the answer to an integer. That means it will drop all decimal points.

Modulus (%): You may think that this is the percent key, but in computer programming it is not! What this key does is it divides two numbers then gives you the remainder. This is a very useful ability! It means that using the following code you are able to tell if an input number is even or odd!



Get it? This works because if a number divided by two gives you a remainder of 0, it must be even! Otherwise... it's odd.

Multiplying and Dividing: In a computer program, you don't use the x key for multiplying, because this could be confused with a letter. Similarly, there is no key that shows the typical divide symbol. Instead, you use the symbol * for multiplying and / for dividing.