Monday, January 23, 2017

Learn Java for Beginner easily ! part 7 : Mathematical Operation

As we know at school that there are 4 basic math operators. They are Addition (+), Substraction (-),Division (/) and Multiplication (*). But in this tutorial I'm gonna teach you guys 5 operators. The fifth one is the Modulus (%).Also I will write the code in new datatype, the new datatype that I mean is Int (Integer). So Let's start.







I have given you guys the example of this math operators in Part 6 . If you guys have seen it. Then there will be not much to explain anymore in this part. Keep scrolling : )










So whenever you want to have any substraction between two variables. Make a new variable to declared the substraction between two variable you want. If you're confuse, look at Line 12 on the picture above.








It is a simple program on the picture above in case if you still need example of it, but what I want to show to you guys is on the picture below.


Q : Why is the result 2 ? we all know that 12 divide by 5 is not equal 2,

A : That's because the data type  is Int (Integer). So whenever you use Int as the data type of your variable, It's just like you tell java that you want the result in Integer format. So if you want to get a more accurate answer of 12/5, change the data type of the "answer" variable into double. Just like what I'm about to show you on the picture below.











You can also change the data type  into double if you want to multiply a fraction (decimal number).









Modulus is the rest of the number you divide by a smaller number. For example, look at the picture below. When I Modulus 30 by 24, the result is 6. That's because 30 divide by 24 is 1, but there are still 6 points leftover. This 6 points that leftover that we called modulus.




If you still don't get it, look at the picture below.





Why is the answer  0 ? Here is the explanation :

We all know that 30 divided by 5 is 6. Because there's no any points leftover (30 will be cleaned out when you divided it by 5) then the modulus is 0.

So that's it guys, Thank you for your visit.
If you got somehing to be asked. Leave a comment below.




No comments:

Post a Comment