operands (modulo)

4
9% 4 is 1 cout yield to display modulu1: the following statement a string and a value, to cout: 17 % 3 is 2 (the remainder when 17 is divided by 3 is 2)

Upload: nicole-ynne-estabillo

Post on 15-Apr-2017

38 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Operands (Modulo)

9% 4 is 1cout yield to display modulu1:

the following statement a string and a value, to cout:

17 % 3 is 2 (the remainder when 17 is divided by 3 is 2)

Page 2: Operands (Modulo)

14 % 2 is 0 (the remainder when 14 is divided by 2 is 0)

Integer Division

The division of two integer values can produce rather strange results for the unwary. For example, the expression 15/2 yields the integer result 7.

Page 3: Operands (Modulo)

Because integers can’t contain a fractional part, the value 7.5 can’t be obtained. The fractional part resulting when two integers are divided—the remainder—is always dropped (truncated).

Therefore 9/4 is 2.And 20/3 is 6.

Page 4: Operands (Modulo)

You need to retain the remainder of an integer division.

C++ provides the modulus operator (also referred to as the “remainder operator”),

This operator captures the remainder when an integer is divided by an integer; using a non-integer value with the modulus operator results in a compiler error.