This article was reviewed by Luigi Oppido and by wikiHow staff writer, Nicole Levine, MFA. Luigi Oppido is the Owner and Operator of Pleasure Point Computers in Santa Cruz, California. Luigi has over 25 years of experience in general computer repair, data recovery, virus removal, and upgrades. He is also the host of the Computer Man Show! broadcasted on KSQD covering central California for over two years.
This article has been fact-checked, ensuring the accuracy of any cited facts and confirming the authority of its sources.
This article has been viewed 141,522 times.
There are two types of division in Java—integer division and floating-point division. Both types use the forward slash (/) symbol as the operator, following the format dividend / divisor.[1] Read on to learn how to divide two integers (non-decimal whole numbers) to receive an integer quotient, and how to use floating point division to get a decimal result.
Steps
Community Q&A
-
QuestionIf 'a' variable is divisible by 2 then print 'a' is even otherwise print odd.JessieCommunity AnswerComplexified: if (a%2 == 0) { System.out.println("even"); } else { System.out.println("odd"); Ternary Operator: System.out.println((a%2 == 0) ? "even" : "odd"); Explanation: "%" is "Modulus", which returns the remainder after dividing the two numbers. Only even numbers can be divided by two without remainder (0). Ternary: You can put ternary operators (([condition]) ? [statement if true] : [statement if false]) in a println() function but you can't put println() functions in ternary operators
Video
Tips
-
When dividing mixed integer and floating point numbers, the floating point values (float or double) are automatically converted to the double type when dividing.[6]Thanks
References
- ↑ https://codegym.cc/groups/posts/integer-division-java
- ↑ https://mathworld.wolfram.com/IntegerDivision.html
- ↑ https://docs.oracle.com/javase/7/docs/api/java/lang/ArithmeticException.html
- ↑ https://github.com/processing/processing/wiki/Troubleshooting
- ↑ https://docs.oracle.com/javase/specs/jls/se15/html/jls-15.html#jls-15.17
- ↑ https://learn.saylor.org/mod/book/tool/print/index.php?id=26807