0% found this document useful (0 votes)
11 views

Module 4 - Numeric Operators - PROG78002 Introduction to Programming With Java

The document explains numeric operators in Java, including addition, subtraction, multiplication, division, and the remainder operator (%). It highlights how integer division truncates the fractional part and provides examples of using the remainder operator to determine even or odd numbers and calculate days of the week. The document emphasizes the importance of floating-point division and the utility of the remainder in programming.

Uploaded by

snrprintinginc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Module 4 - Numeric Operators - PROG78002 Introduction to Programming With Java

The document explains numeric operators in Java, including addition, subtraction, multiplication, division, and the remainder operator (%). It highlights how integer division truncates the fractional part and provides examples of using the remainder operator to determine even or odd numbers and calculate days of the week. The document emphasizes the importance of floating-point division and the utility of the remainder in programming.

Uploaded by

snrprintinginc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

2/6/2020 Numeric Operators - PROG78002 Introduction to Programming with Java

NUMERIC OPERATORS

Operations Performed in a Java Program

The operators for numeric data types include the standard arithmetic operators:
addition (+), subtraction (-), multiplication (*), division (/) and remainder (%).

When both operands of a division are integers, the result of the division is the
quotient and the fractional part is truncated.

For example 5 / 2 yields 2 not 2.5 and -5 / 2 yields -2 not -2.5

To perform a floating point division, one of the operands must be a floating-


point number.

For example 5.0 / 2 yields 2.5

The % operator is known as remainder or modulo operator and yields the


remainder after division. The operand on the left is the dividend and the
operand on the right is the divisor.

For example 7 % 3 yields 1, 3 % 7 yields 3, 12 % 4 yields 0, 26 % 8


yields 2, 20 % 13 yields 7

The remainder is very useful in programming.

For example a

number % 2 will always yield 0 if number is even

Number % 2 will always yield 1 if number is odd

https://slate.sheridancollege.ca/d2l/le/content/657541/viewContent/8995324/View 1/2
2/6/2020 Numeric Operators - PROG78002 Introduction to Programming with Java

Another use of the remainder is used to determine the day of the week.

For example if today is Saturday, it will be Saturday again in 7 days.

If today is Saturday (the 6th day of the week) and you were going to meeting 7 days
from now you will meet on Saturday ( the 6th day) again.

(6+7) %7 = 6

If you were to meeting someone in 10 days from now, given that today is Saturday,
what day will it be.

Here is the calculation.

Sheridan College. Faculty of Continuing and Professional Studies.

https://slate.sheridancollege.ca/d2l/le/content/657541/viewContent/8995324/View 2/2

You might also like