Download Article
Download Article
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] X Research source 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
Search
-
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
Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
Advertisement
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] X Research sourceThanks
Submit a Tip
All tip submissions are carefully reviewed before being published
Name
Please provide your name and last initial
Thanks for submitting a tip for review!
Advertisement
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
About This Article
Thanks to all authors for creating a page that has been read 141,591 times.
Advertisement