#7 Mathematical Operators in java
Mathematical Operators
As we saw in the preceding example there are particular symbols used to represent operators when performing calculations:
Styling Tables
Opeerator | Description | Example – given a is 15 and b is 6 |
---|---|---|
+ | Addition | a + b, would return 21 |
- | Subtraction | a - b, would return 9 |
* | Multiplication | a * b, would return 90 |
/ | Division | a / b, would return 2 |
% | Modulus | a % b, would return 3 (the remainder) |
class Example4
{
public static void main(String args[])
{
int iresult, irem;
double dresult, drem;
iresult = 10 / 3;
irem = 10 % 3;
dresult = 10.0 / 3.0;
drem = 10.0 % 3.0;
System.out.println("Result and remainder of 10 / 3: " + iresult + " " + irem); System.out.println("Result and remainder of 10.0 / 3.0: " + dresult + " " + drem);
}
}
Predicted Output:
Result and Remainder of 10/3: 3 1
Result and Remainder of 10.0/3.0: 3.3333333333333335 1
The difference in range is due to the data type since ‘double’ is a double precision 64-bit floating point value.
Comments
Post a Comment