Forgot username/password? Don't have an account?
Login with Facebook
Home » Courses » Coding Basics 2 » Deeper Look: Math Operators
Subscribe for updates.
* New to us? log in with Facebook and get a free day pass

Deeper Look: Math Operators

Things are starting to fall into place: let's revisit the math operators and add a few new tricks and shortcuts. After taking a deeper look at defining and assigning variables, now it's time to go for Math Operators. Learning various types of math operators is necessary for anyone who wants to develop as a programmer. We have seen all types of arithmetic operators such as +, -, *, /, but we need to know about one more very important operator to use in a number of programs. it's defined as modulo and is abbreviated with the percentile symbol %.

Percentile (%) Operator

If we use this symbol for a and b: a%b; it divides a by b to the closest integer quotient. The remainder is left over to be part of modulo. Hence, we can say that % is the operator which gives the reminder as output provided the quotient is an integer. For example, if we have 13%2, here the best possible division that can be performed keeping the quotient to an integer value is (13/2)=(6+remainder.) We can see that the quotient can't be 6.5 because it isn't an integer value, so it's limited to 6 with a remainder of 1. Therefore, 1 is considered to be the modulus of the equation 13%2. Your turn! What is 9%4? (9/4 = 2 with a remainder of 1: the answer is 1.)

Consider another example where the dividend is an even number: 12%2;. Here, division can be performed to the fullest extent and still the quotient is an integer. Therefore, this leaves a remainder of 0, which is the final value of 12%2. This operator is very useful not only for finding the remainder but also in various programs for finding Fibonacci series, whether a number is odd or even, etc. You will understand the use of this operator more as you move forward and start utilizing it while coding.

Incrementing and decrementing variables

Since we learned all the basic operators and their uses, we need to focus on how these operators are used in a program. We can use these operators and write the equations in the same way as we write in mathematical algebra. But there are a few shortcuts for writing math equations in programming which makes life easier for the user. Mostly we use math operators for incrementing or decrementing variables and their values, for example: i=i+1.; This will increment the value of i to i+1 before moving to the next line. When we start writing the code, we can minimize this equation and simply write i++; which has the same meaning. This is applicable for all types of operators such as i**; i- -; . Your turn! How would you easily write code to divide I by 3?

If you answered I/=3; you win that one! These are also very useful while writing the conditions for controlled loops where we can either decrement the value using i-- or increment the value using i++.

Got A Question?

'I love being Prime'

Have a question but don't want to ask it publicly? If you are a prime member just ask right here and we will get back to you within 48 hours. Not prime yet? no worries you can ask publicly below.

Act now while we have available seating

Our on line school is private, affordable and interactive with trainer support.
Act now save your seat before someone else takes yours.
Act now